Welcome to WebmasterWorld Guest from 54.224.57.95

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

str_replace a very simple but tricky issue

     
6:20 pm on Jan 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Folks,

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?

8:53 pm on Jan 14, 2006 (gmt 0)

10+ Year Member



One solution would be to do two strreplace calls on "test", then "Test"

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...

9:09 pm on Jan 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey There

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

9:24 pm on Jan 14, 2006 (gmt 0)

10+ Year Member



I believe the following should do what you want.

$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

12:03 am on Jan 15, 2006 (gmt 0)

10+ Year Member



Yeah - I know it was not a scaleable solution - you should have said you had loads of similar cases to consider: :-)

Give the pattern matching/substitution a go as suggested in the previous post - looks like a good bet!

2:38 am on Jan 15, 2006 (gmt 0)

10+ Year Member



there is no such function as pcre_replace(). All PCRE functions in PHP are preg_* so it is preg_replace()
8:59 am on Jan 15, 2006 (gmt 0)

10+ Year Member



Oops - small typo. You're quite right, the function is called preg_replace, although the package is called PCRE (Perl-Compatible Regular Expressions)! Cautionary note: never answer coding questions late on a Saturday night after a few glasses of wine!
9:32 am on Jan 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lol nice note!

leme check code, i will get back soon .

5:58 pm on Jan 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is just the above suggestion with a twist =)

$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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month