Code: Select all
$haystack='This is <a href=http://lattilad.org>an html formatted link</a> in an html text.';
$needle='html';
$replace='<a href=en.wikipedia.org/wiki/HTML>HTML</a>';
$starttag='<a ';
$endtag='>';
In the real application, I have to take three pairs of starttag–endtag in account: angle brackets to avoid replacing html code, A tags to avoid replacing text that is a link already (our purpose is to create links in a text automatically) and a special BBCode link pair to be created to suppress link creation.
What makes things harder is that, as you can see in the example, starttag and endtag may precede and follow needle from a distance, so searching for something like $starttag.$needle.$endtag won’t work.
I’ll appreciate any help. Thank you.