Looking for a way to find and replace word in a text and then insert some html code in the text to form a new array. Short explanation: I have a text in a query selected from a database using sql which I call to show on screen with the following operator: <? echo $page['description']; ?> This calls for instance on screen: blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla In the sql database theres also information about the url, which I show with this operator: <? echo $page['url'];?> Lets say thats digitalpoint.com What I want to do is the following: In the text I want to be able to use parameters to put around words in the text. So that would look like this: (stored in the sql database) blablabla blablabla blablabla {blablabla blablabla} blablabla blablabla blablabla Then the words between {} should use the href information and use the sql information $page['url']; and they should form a new array so it can be shown on screen, so I would get this: blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla Can this be done?
Look at the PHP manuals for str_replace or preg_replace. You'd use str_replace if you have a simple fixed string that you want to replace with another and preg_replace if you need to use regular expressions to define your target.
Thanks for the suggestion jnestor, I've looked into the str_replace but this doesnt seems to fit my problem, because I'm dealing with a lot of text and every text is different and every word combination is different. For instance: Text: I have a text written about a certain topic that is of interest to every webmaster. Url: http://www.digitalpoint.com/ Now I want to use the text webmaster to link to digitalpoint, in the text I'll set parameters to indicate thats the text that should be linked to digitalpoint, for instance with {} but also could use %% or any other parameter, as long a I can select the word (words) to be linked to. New database information, done manually in the database. Text: I have a text written about a certain topic that is of interest to every {webmaster}. url: http://www.digitalpoint.com/ This would be done using some php code to form a new array and call it on screen, that could look like this: I have a text written about a certain topic that is of interest to every webmaster. I'm ready to pay if somebody comes with a ready to use solution. Thx.
If you mean you want to replace any word inside { } with a link then you can do this: $string = "I have a text written about a certain topic that is of interest to every {webmaster}"; $url = "http://www.digitalpoint.com/"; $new_str = preg_replace('/{(.*)}/','<a href="'.$url.'">$1</a>',$string); echo "string: $string<br/>"; echo "new_str: $new_str<br/>"; Code (markup): Beware though, this won't work if you have more than one target in your string. You'll need to refine the rules for the regular expression depending on exactly what you want to accomplish.
Thanks! This is exactly what I've been looking for. Not really sure by what you mean with more than one target. But I'll try to implement it in the website I've been workin on. Much appreciated.
If your initial string is "I have a text written by an {expert} that is of interest to every {webmaster}", the above won't work. It'll link everything from the first { to the last }. That's what I mean be needing to refine your rules a bit in that case. If it's exactly one word in braces each time you can do it one way. If it could be more than one word you'd do it another way etc.