Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Where am I going wrong with this preg-match?

Posted on 2015-01-05
4
Medium Priority
?
125 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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 75

Accepted Solution

by:
käµfm³d   👽 earned 1200 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

824 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