Re: preg_replace... I can't do it!! [message #180525 is a reply to message #180511] |
Sat, 23 February 2013 22:48 |
cate
Messages: 12 Registered: January 2012
Karma:
|
Junior Member |
|
|
On Feb 22, 3:57 pm, mamboo <mambooo> wrote:
> Hello... I have a problem with preg_replace...
>
> 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
> href="http://mysite.com/Lucas.html?sessid=0343erererere34343">Lucas</a>
> and this ..."
>
> can you help me?
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
href="$6">$5</a>!i;
You can cut it down further...(leave some eye "anchors" to help people
read it)
$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.
|
|
|