Match given keyword but ignore text between <h1></h1> tags
Moderator: General Moderators
Match given keyword but ignore text between <h1></h1> tags
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.
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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Match given keyword but ignore text between <h1></h1> tags
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
Hey frnd,
I'm sorry....it worked...
I was using it wrongly like /<expr>/
Thx a bunch
I'm sorry....it worked...
I was using it wrongly like /<expr>/
Thx a bunch
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Match given keyword but ignore text between <h1></h1> tags
I thought so.urnetmate wrote:Hey frnd,
I'm sorry....it worked...
I was using it wrongly like /<expr>/
No problem.urnetmate wrote:Thx a bunch
Difficulty in matching the headings with css class
Not matching this case :
Code: Select all
<h1 class="abc">test</h1>
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Difficulty in matching the headings with css class
That's because you didn't mention such a tag in your original post.urnetmate wrote:Not matching this case :
Code: Select all
<h1 class="abc">test</h1>
Re: Match given keyword but ignore text between <h1></h1> tags
I have updated regular expression like :
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....
Code: Select all
$regex = "#\b".$needle_s."\b(?=((?!</h[12]>).)*(<h[12]\s+.*>|$))#is";
But now the issue is with the subject having multiple <h1> & <h2> tags. It ignores everything between <h1> & </h2>.
Please suggest....
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Match given keyword but ignore text between <h1></h1> tags
Some examples?urnetmate wrote:...
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
For e.g.
Suggest Regular Expression to match the "$needle" in $subject.
The Regx should not match the $needle in <h1> and <h2> tag.
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>';
The Regx should not match the $needle in <h1> and <h2> tag.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Match given keyword but ignore text between <h1></h1> tags
Okay, I understand.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>';
You mean "please suggest..."? This is not a free programming service, you know!urnetmate wrote:Suggest Regular Expression to match the "$needle" in $subject.
The Regx should not match the $needle in <h1> and <h2> tag.
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!