By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Regex with HTML in PHP

P: 1
Hi,

I have the following html code and need to display all contents within the <h4> tags, except the one containing "Footer". I am writing a parser script in php to transform an old to a new html layout.

The old layout is basically like this:

<h4>Background</h4>
<h4>Advice</h4>
<h4>Footer</h4>
<h4>Support</h4>

And my regex looks like this:

$regex_newsections = "/<h4>(.*?)(?=<\/h4>)/m";

I need the brackets surrounding .*? because I will use the contents later on in the preg_match_all function.

I have tried using a negative lookahead but none of the following work:
$regex_newsections = "/<h4>(.*?)(?!Footer)(?=<\/h5>)/m";
$regex_newsections = "/<h4>((?!Footer).*?)(?=<\/h5>)/m";
$regex_newsections = "/<h4>((?!Footer)*?)(?=<\/h5>)/m";

Does anyone have any ideas? Cheers.
Feb 10 '08 #1
Share this Question
Share on Google+
1 Reply


ronverdonk
Expert 2.5K+
P: 4,258
Hi,

I have the following html code and need to display all contents within the <h4> tags, except the one containing "Footer". I am writing a parser script in php to transform an old to a new html layout.

The old layout is basically like this:

<h4>Background</h4>
<h4>Advice</h4>
<h4>Footer</h4>
<h4>Support</h4>

And my regex looks like this:

$regex_newsections = "/<h4>(.*?)(?=<\/h4>)/m";

I need the brackets surrounding .*? because I will use the contents later on in the preg_match_all function.

I have tried using a negative lookahead but none of the following work:
$regex_newsections = "/<h4>(.*?)(?!Footer)(?=<\/h5>)/m";
$regex_newsections = "/<h4>((?!Footer).*?)(?=<\/h5>)/m";
$regex_newsections = "/<h4>((?!Footer)*?)(?=<\/h5>)/m";

Does anyone have any ideas? Cheers.
To start with the 3 instructions beginning at:
[php]
$regex_newsections = "/<h4>(.*?)(?!Footer)(?=<\/h5>)/m";
[/php]
all search for </h5> as the closing tag.

Ronald
Feb 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.