Solved

Where am I going wrong with this preg-match?

Posted on 2015-01-05
4
107 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 109

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 75

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to dynamically set the form action using jQuery.

821 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