Solved

Where am I going wrong with this preg-match?

Posted on 2015-01-05
4
97 Views
Last Modified: 2015-01-06
I am trying to pull all of the data between two <div> tags and I am getting the following error:

Notice: Undefined offset: 1.

What am I doing wrong?

preg_match('%<div class="productdescription" itemprop="description">(.*?)<\/div>%i',$buffer,$matches16);
  
        $LongDesc=$matches16[1];		 

echo $LongDesc."<BR>";	

Open in new window



Here is the description I am attempting to get:

START HERE ---->   <div class="productdescription" itemprop="description">
                <p>
    Built with assistance from bike patrol officers nationwide, the 5.11 Bike Patrol Polo was designed to provide bike mounted officers with a safe, reliable,
    and thoroughly professional alternative to baggy or constricting police bike patrol shirts. Crafted from lightweight, breathable, high performance jersey
    knit polyester, the 5.11 Bike Patrol Polo offers superior moisture wicking performance, quick drying action, and anti-microbial characteristics that keep
    you looking and feeling your best all day long. Integrated reflective tape increases visibility, an underarm gusset ensures full freedom of movement, a
    generous length and drop tail ensures your bike polo stays tucked in, and a mic loop at the placket keeps you connected. Best of all, the bright colors on
    our bike patrol shirts are colorfast against perspiration and sunlight, ensuring a clean, professional appearance throughout your shift.
</p>
<ul>
    <li>
        Chest Pen Pocket
    </li>
    <li>
        Underarm gusset for mobility
    </li>
    <li>
        Integrated reflective tap
    </li>
    <li>
        Moisture wicking, quick drying, anti-microbial
    </li>
    <li>
        Mic loop at front placket
    </li>
    <li>
        Colorfast to Light &amp; Perspiration
    </li>
    <li>
        Jersey knit polyester
    </li>
    <li>
        Performance Fit
    </li>
    <li>
        Generous Length
    </li>
    <li>
        Drop tail
    </li>
</ul>
            </div>     <---   END HERE

Open in new window

0
Comment
Question by:lawrence_dev
4 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 40532811
This will get the data.  Sometimes the right answer is not found in regular expressions, which can be devilishly difficult to get right, even for the most experienced programmer.
http://iconoun.com/demo/temp_lawrence_dev.php

<?php // demo/temp_lawrence_dev.php

/**
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28591233.html
 */
error_reporting(E_ALL);

/**
 * ISOLATE TEXTS FROM HTML DIV TAGS
 *
 * REFERENCE:
 * http://php.net/manual/en/function.explode.php
 *
 */

/**
 * DELIMITERS
 */
$aaa = '<div class="productdescription" itemprop="description">';
$zzz = '</div>';

/**
 * THE MOST IMPORTANT THING A PROGRAMMER CAN HAVE -- SOME TEST DATA
 */
$htm = <<<EOF
START HERE ---->
<div class="productdescription" itemprop="description">
                <p>
    Built with assistance from bike patrol officers nationwide, the 5.11 Bike Patrol Polo was designed to provide bike mounted officers with a safe, reliable,
    and thoroughly professional alternative to baggy or constricting police bike patrol shirts. Crafted from lightweight, breathable, high performance jersey
    knit polyester, the 5.11 Bike Patrol Polo offers superior moisture wicking performance, quick drying action, and anti-microbial characteristics that keep
    you looking and feeling your best all day long. Integrated reflective tape increases visibility, an underarm gusset ensures full freedom of movement, a
    generous length and drop tail ensures your bike polo stays tucked in, and a mic loop at the placket keeps you connected. Best of all, the bright colors on
    our bike patrol shirts are colorfast against perspiration and sunlight, ensuring a clean, professional appearance throughout your shift.
</p>
<ul>
    <li>
        Chest Pen Pocket
    </li>
    <li>
        Underarm gusset for mobility
    </li>
    <li>
        Integrated reflective tap
    </li>
    <li>
        Moisture wicking, quick drying, anti-microbial
    </li>
    <li>
        Mic loop at front placket
    </li>
    <li>
        Colorfast to Light &amp; Perspiration
    </li>
    <li>
        Jersey knit polyester
    </li>
    <li>
        Performance Fit
    </li>
    <li>
        Generous Length
    </li>
    <li>
        Drop tail
    </li>
</ul>
            </div>     <---   END HERE
EOF;

/**
 * PERFORM THE MATCH AND SHOW THE WORK PRODUCT
 */
$arr = explode($aaa, $htm);
$arr = explode($zzz, $arr[1]);
$mat = $arr[0];

echo '<pre>';
echo PHP_EOL;
echo htmlentities($mat);

Open in new window

HTH, ~Ray
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 40532824
Most likely, you need to enable "dot matches all" mode (commonly referred to as "single-line" mode). You do that via the "s" modifier:

preg_match('%<div class="productdescription" itemprop="description">(.*?)<\/div>%si',$buffer,$matches16);

Open in new window

0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 150 total points
ID: 40533102
To cover the case where no match is found, just use the return value from preg_match:
if (preg_match(...) ) {
#great, do something with $matches16 or whatever
} else {
#oops no match was found
}

Open in new window

0
 

Author Closing Comment

by:lawrence_dev
ID: 40533873
Kaufmed & Terry,  Thanks!  Easy to understand.  

Ray, Way too technical and difficult to understand.  The more simple it is, the easier it is to understand, and build on.  If I could understand the examples on php.net, I would not need Experts-Exchange!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now