Solved

Where am I going wrong with this preg-match?

Posted on 2015-01-05
4
111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 110

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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.

752 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