Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP: preg_match_all parse error

Posted on 2011-02-21
4
Medium Priority
?
274 Views
Last Modified: 2012-05-11
My code gives a parse error and I cannot find the problem.
<pre><?php

$html = '
<div>
 <div>
  <p>
   <strong>Hello
  </p>
 </div>
';

echo htmlentities(checkTags('div',$html));

function checkTags( $tag, $xml ) {
  $tag = preg_quote($tag);
  preg_match_all('{<'.$tag.'[^>]*>(.*?)</'.$tag.'>.'}', $xml, $matches, PREG_PATTERN_ORDER);
  return $matches[1];
}

?></pre>

Open in new window

0
Comment
Question by:hankknight
[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
  • 2
4 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 34944120
Count the single quotes in your regex on line 16
0
 
LVL 16

Author Comment

by:hankknight
ID: 34944250
Thanks, I don't know where to add or remove the single quote.

I found that code on this page:
http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers
<pre><?php

$html = '
<div>
 <div>
  <p>
   <strong>Hello
  </p>
 </div>
';

$zzz=checkTags('div',$html);
print_r($zzz);

function checkTags( $tag, $xml ) {
  $tag = preg_quote($tag);
  preg_match_all('{<'.$tag.'[^>]*>(.*?)</'.$tag.'>}', $xml, $matches, PREG_PATTERN_ORDER);
  return $matches[1];
}

?></pre>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34944422
Can you tell us in plain language what you want that regular expression to do for you?  There might be a better way!
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1600 total points
ID: 34944877
I would GUESS that the pattern should be

'{<'.$tag.'[^>]*>(.*?)</'.$tag.'>}'
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

719 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