Solved

PHP: preg_match_all parse error

Posted on 2011-02-21
4
259 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
  • 2
4 Comments
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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 110

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 400 total points
ID: 34944877
I would GUESS that the pattern should be

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 look for a specific file type in a local or remote server directory using PHP.

679 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