Page 1 of 1

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

Posted: Fri Jan 09, 2009 1:50 am
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.

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

Posted: Fri Jan 09, 2009 3:15 am
by prometheuzz
Try this (untested!) snippet:

Code: Select all

$subject = preg_replace('#test(?=((?!</h1>).)*(<h1>|$))#is', $replacement, $subject);

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

Posted: Fri Jan 09, 2009 3:33 am
by urnetmate
Not Working...
:?

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

Posted: Fri Jan 09, 2009 3:41 am
by urnetmate
Hey frnd,

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

Thx a bunch :D

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

Posted: Fri Jan 09, 2009 3:48 am
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.

Difficulty in matching the headings with css class

Posted: Mon Jan 12, 2009 4:35 am
by urnetmate
Not matching this case :

Code: Select all

 
<h1 class="abc">test</h1>
 

Re: Difficulty in matching the headings with css class

Posted: Mon Jan 12, 2009 6:43 am
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.

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

Posted: Wed Feb 04, 2009 12:26 am
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....

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

Posted: Wed Feb 04, 2009 1:15 am
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?

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

Posted: Wed Feb 04, 2009 1:40 am
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.

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

Posted: Wed Feb 04, 2009 3:27 am
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!