Forum Moderators: coopster
its a very small issue but i couldnt find a way out of this, could anyone give a hint?
problem is:
lets say you have a paragraph which says
$para="This is a test paragraph and i am using it for TEST purpose.";
now lets say, i want to link all occurences of word "Test" in that paragraph to a url
for example if i want all "Test" to be link to [test.com...]
now if i do it like this
$keyword="Test";
$para=str_replace($keyword,"<a href=http://test.com>$keyword</a>",$para);
now there the issue arrises, that thing is case sensitive if i specify "Test" as keyword, it would only link Test and wont link "test" or "TEST" now if somehow i make it case-insensitive then another issue arrises and that is the real problem i am looking to solve
that problem is, if the replace function is case-insensitive then it would then it would link all of those "test" occurences using SAME anchor for example
$para="This is a test paragraph and i am using it for TEST purpose. This is another Test";
$keyword="Test";
then after a case insensitive replace it would become
$para="This is a <a href=http://test.com>Test</a> paragraph and i am using it for <a href=http://test.com>Test</a> purpose. This is another <a href=http://test.com>Test</a>" ;
whereas i want it to be like this
$para="This is a <a href=http://test.com>test</a> paragraph and i am using it for <a href=http://test.com>TEST</a> purpose. This is another <a href=http://test.com>Test</a>" ;
know what i mean? its simply like that the conversion of a word into link should be case insensitive but but but case of that word shall remain exactly the same as it was before linking.
I guess i am not clear?
Bear in mind that this will introduce extra "test" strings on the first replace, so you might have to use a guaranteed unique string in each case, then a final replace to replace those!
eg
replace("test" with <unique string1>)
replace "Test" with <unique string2>)
Then:
replace (<unique string1> with your URL with "test")
replace (<unique string2> with your URL with "Test")
There is probably a better way, but this is the first one I thought of...
Thanks for your message but unfortunately that cannot be done because of following reasons.
A paragraph could be as big as 2 pages and there can be as more as 100 of words to be made links and we would have list of those words in one case only, i mean we wont be able to have each word repeatedly replaced for each case so there has to be a way to make links for all "Test" in one statement and also keep the anchors of those links same as they were.
Looking for ur reply
$pattern="/(test)/i"; // case-insensitive pattern
$replace="<a href='url'>$1</a>";
pcre_replace($pattern,$replace,$para);
pcre_replace substitutes a string into another where the pattern matches - here we use a case-insensitive match of the word "test" into the paragraph, but remember the part of the string that matches the word test (including its lower/upper-case letters). We then build the replacement string using this plus the anchor tag (replace 'url' with the actual url). Then apply pcre_replace.
Haven't actually tested this, but this should work.
Simon
$para="This is a test paragraph and i am using it for TEST purpose. This is another Test";
$word="Test";
$linked_para=preg_replace("/(".$word.")/i", "< a href=\"".strtolower($word)."\" > $1"< /a >, $para);
echo $linked_para;
Hope this gives you some ideas.
Justin