homepage Welcome to WebmasterWorld Guest from 23.22.194.120
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
str_replace a very simple but tricky issue
Anyango




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

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?

 

FalseDawn




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

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

Anyango




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

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

simon2263




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

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

FalseDawn




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

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!

madmac




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

there is no such function as pcre_replace(). All PCRE functions in PHP are preg_* so it is preg_replace()

simon2263




msg:1276987
 8:59 am on Jan 15, 2006 (gmt 0)

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!

Anyango




msg:1276988
 9:32 am on Jan 15, 2006 (gmt 0)

lol nice note!

leme check code, i will get back soon .

jd01




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved