eBay - Gold WOW - Chomsky - Mortgages - Loans

PDA

View Full Version : preg_replace Help


hrock
Jan 3rd 2008, 4:19 am
hi,

I use Preg_replace to automatically links some keywords


Here is my code

$texte = preg_replace('`\b((keyword)s?)\b`si','<a href="../keyword.html">$1</a>',$texte);

It workes great but when my keyword is inside an element like <a href="zzz"> </a> it breaks the links.

My questions is how can i do that preg_replace will note replace any keywords inside an element?

And a second question how can i do that preg_replace dosen't match when there is an - directly before my keyword?

I hope that you all understand my english

Thanks for your help

LittleJonSupportSite
Jan 3rd 2008, 11:31 am
Modify this as needed.


$String="<a href='blah.com'>welcome to blah</a>";
$msgStrip = preg_replace('/<a\s+.*?[href=]["|\']([^"\']+)["|\']>
{1}([^<]+)<\/a>/is', '\2 (\1)',$String);

it will output welcome to blah (blah.com)


If you like what I have posted feel free to drop me some reputation =)

Hope this helps and Happy New year

pratham
Jan 4th 2008, 8:25 am
My questions is how can i do that preg_replace will note replace any keywords inside an element?

preg_replace ('`[^"\'](keywords?)[^"\']`si', '<a href="../keyword.html">$1</a>');

This looks for quotes (single or double) before and after the keyword(s) and does not match if there are.


And a second question how can i do that preg_replace dosen't match when there is an - directly before my keyword?


preg_replace ('`[^-](keywords?)`si', '<a href="../keyword.html">$1</a>');