Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- preg_replace... I can't do it!!
February 22, 2013, 9:57 pm
rate this thread
In a long text I have some links...
"My name is <link>Lucas|http://mysite.com/Lucas.html </link> and this is
my <link>car|http://mysite.com/my_car.html </link> so..."
using preg_replace I would like to transform the text before it would be
displayed to add a session_id.
It would become ""My name is <a
and this ..."
can you help me?
Re: preg_replace... I can't do it!!
I'm just learning php myself, but here's some rx to get you started
(just use the rx part - this is a perl construct) I think php has a
perl rx engine in it.
$s1 =~ s!(My name is )<link>(.*?)\|(http:.*?)</link>( and this is
my )<link>(car)\|(http:.*?)</link>.*!$1<a href="$3">$2</a>$4<a
You can cut it down further...(leave some eye "anchors" to help people
$s1 =~ s!.*?<link>(.*?)\|(http:.*?)</link>.*?(http:.*?)</link>.*!My
name is <a href="$2">$1</a> and this is my <a href="$3">car</a>!i;
Just as a matter of maintenance sanity however, I'd break up your
source string, forget the substitution, and reassemble it. These are
fun to write, but long substitutions can be brittle and difficult for
others to read.
- Captain Paralytic
February 25, 2013, 10:43 am