Match given keyword but ignore text between <h1></h1> tags

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
urnetmate
Forum Commoner
Posts: 27
Joined: Wed Sep 27, 2006 1:09 am

Match given keyword but ignore text between <h1></h1> tags

Post by urnetmate »

Hi all,

I want to replace keywords with hyperlinks. The content has html tags.
For eg.
<?php
$subject = "Here is a <h1>test subject</h1><br>The test page contents are as below.....";
?>

In the above case I want to hyperlink the keyword 'test', then it should not match the test between <h1></h1>.

But should match the keyword in "The test page contents are as below....."
Please suggest the regular expression to match this case.

Thanks in advance.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by prometheuzz »

Try this (untested!) snippet:

Code: Select all

$subject = preg_replace('#test(?=((?!</h1>).)*(<h1>|$))#is', $replacement, $subject);
urnetmate
Forum Commoner
Posts: 27
Joined: Wed Sep 27, 2006 1:09 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by urnetmate »

Not Working...
:?
urnetmate
Forum Commoner
Posts: 27
Joined: Wed Sep 27, 2006 1:09 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by urnetmate »

Hey frnd,

I'm sorry....it worked...
I was using it wrongly like /<expr>/

Thx a bunch :D
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by prometheuzz »

urnetmate wrote:Hey frnd,

I'm sorry....it worked...
I was using it wrongly like /<expr>/
I thought so.
urnetmate wrote:Thx a bunch :D
No problem.
urnetmate
Forum Commoner
Posts: 27
Joined: Wed Sep 27, 2006 1:09 am

Difficulty in matching the headings with css class

Post by urnetmate »

Not matching this case :

Code: Select all

 
<h1 class="abc">test</h1>
 
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Difficulty in matching the headings with css class

Post by prometheuzz »

urnetmate wrote:Not matching this case :

Code: Select all

 
<h1 class="abc">test</h1>
 
That's because you didn't mention such a tag in your original post.
urnetmate
Forum Commoner
Posts: 27
Joined: Wed Sep 27, 2006 1:09 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by urnetmate »

I have updated regular expression like :

Code: Select all

 
$regex = "#\b".$needle_s."\b(?=((?!</h[12]>).)*(<h[12]\s+.*>|$))#is"; 
 
to ignore keywords between <h1></h1> and <h2></h2> tags.

But now the issue is with the subject having multiple <h1> & <h2> tags. It ignores everything between <h1> & </h2>.
Please suggest....
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by prometheuzz »

urnetmate wrote:...
But now the issue is with the subject having multiple <h1> & <h2> tags. It ignores everything between <h1> & </h2>.
Please suggest....
Some examples?
urnetmate
Forum Commoner
Posts: 27
Joined: Wed Sep 27, 2006 1:09 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by urnetmate »

For e.g.

Code: Select all

 
$needle = "this keyword phrase";
$subject = 'Xxx xxx xxx<h1 class="main_h">XXxx xxx xxxxxxxx xxxxx</h1><p>Xxxxxxxx xxx x xxx x x  x x xxxxx  x x x x match this keyword phrase xx xxxxxx xx xxxxxx xxxxx</p><h2><span class="sub_h">x xxx do not match this keyword phrase xx x xxxxx</span> </h2><ol><li>Xxxx xxxx xx xxxxxxx this keyword phrase xxxxxx xxx xxxxxxxxx xxxxxxxxx xxxxxxx xxx xxxxx.</li><li>Xxxxx xxxxx xx.</li></ol>';
 
 
Suggest Regular Expression to match the "$needle" in $subject.
The Regx should not match the $needle in <h1> and <h2> tag.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Match given keyword but ignore text between <h1></h1> tags

Post by prometheuzz »

urnetmate wrote:For e.g.

Code: Select all

 
$needle = "this keyword phrase";
$subject = 'Xxx xxx xxx<h1 class="main_h">XXxx xxx xxxxxxxx xxxxx</h1><p>Xxxxxxxx xxx x xxx x x  x x xxxxx  x x x x match this keyword phrase xx xxxxxx xx xxxxxx xxxxx</p><h2><span class="sub_h">x xxx do not match this keyword phrase xx x xxxxx</span> </h2><ol><li>Xxxx xxxx xx xxxxxxx this keyword phrase xxxxxx xxx xxxxxxxxx xxxxxxxxx xxxxxxx xxx xxxxx.</li><li>Xxxxx xxxxx xx.</li></ol>';
 
 
Okay, I understand.
urnetmate wrote:Suggest Regular Expression to match the "$needle" in $subject.
The Regx should not match the $needle in <h1> and <h2> tag.
You mean "please suggest..."? This is not a free programming service, you know!

I like to help people learn a bit more about regex, but you will have to show some effort on your part that you have tried to find the answer yourself. In the beginning I thought you knew a bit regex, but now it seems like you're only looking for a copy-and-paste solution. Don't get me wrong, you have every right to post these type of "questions" here, but I am not interested in providing that kind of "help".

So, if you want me to help, then please post how you tried to solve this yourself and explain where things went wrong. If you have absolutely no idea where to begin solving this, I suggest you read a book about regex or wait for someone who wants to spoon feed you the answer.

Whatever you do, good luck!
Post Reply