Jump to content


Photo

str_replace or ... ?


  • Please log in to reply
3 replies to this topic

#1 ibzi

ibzi
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 March 2006 - 12:24 AM

Let's say I have a paragraph of text.


"This forum is phpfreaks.com and the person posting this message goes by the Username of ibzi. This is just a paragraph to show an example to the fellow helpers."

Now lets say I want to replace all the occurence of the word "and" regardless about the case. It can be And or AND or aND etc.

How would i do this? I want to replace it with something like:

and >>>>>> <a target="_blank" href="search.php?q=and">and</a>

I tried preg_match but I dont know how to use the function, i constantly get wrong result near the "_blank".

Please help.. it will be appreciated.
If you need some quick PHP coding help then ask me at ibzi.net!
http://www.ibzi.net/php/help

#2 greycap

greycap
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 08 March 2006 - 03:48 AM

[!--quoteo(post=352692:date=Mar 7 2006, 06:24 PM:name=ibzi)--][div class=\'quotetop\']QUOTE(ibzi @ Mar 7 2006, 06:24 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Let's say I have a paragraph of text.
"This forum is phpfreaks.com and the person posting this message goes by the Username of ibzi. This is just a paragraph to show an example to the fellow helpers."

Now lets say I want to replace all the occurence of the word "and" regardless about the case. It can be And or AND or aND etc.

How would i do this? I want to replace it with something like:

and >>>>>> <a target="_blank" href="search.php?q=and">and</a>

I tried preg_match but I dont know how to use the function, i constantly get wrong result near the "_blank".

Please help.. it will be appreciated.
[/quote]

You definately want regular expressions. Try preg_replace though. There are plenty of regular expression generators to help you get it just right.

#3 ibzi

ibzi
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 March 2006 - 10:09 PM

[!--quoteo(post=352734:date=Mar 7 2006, 10:48 PM:name=greycap)--][div class=\'quotetop\']QUOTE(greycap @ Mar 7 2006, 10:48 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You definately want regular expressions. Try preg_replace though. There are plenty of regular expression generators to help you get it just right.
[/quote]

I've been looking for generators for quite awihle now. Any chance anyone can point me to a direction?
If you need some quick PHP coding help then ask me at ibzi.net!
http://www.ibzi.net/php/help

#4 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 08 March 2006 - 11:07 PM

You could do it with a regex or you could do it with a simple str_replace()

$data = str_replace(array("and", "AND"), "<a href......>", $text);

if however the and might be mixed case you would want to look at regexes, which sadly I am rubbish at :)
call me a safe bet, i'm betting i'm not




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users