Solved

PHP: preg_match_all parse error

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
Count the single quotes in your regex on line 16
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
I would GUESS that the pattern should be

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now