Solved

PHP: preg_match_all parse error

Posted on 2011-02-21
4
268 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 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

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…
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 look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

626 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