FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » preg_replace... I can't do it!!
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
preg_replace... I can't do it!! [message #180511] Fri, 22 February 2013 21:57 Go to next message
mamboo is currently offline  mamboo
Messages: 2
Registered: February 2012
Karma: 0
Junior Member
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?
Re: preg_replace... I can't do it!! [message #180525 is a reply to message #180511] Sat, 23 February 2013 22:48 Go to previous messageGo to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
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.
Re: preg_replace... I can't do it!! [message #180540 is a reply to message #180511] Mon, 25 February 2013 10:43 Go to previous message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Feb 22, 9: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?

Regular expressions can be a bit daunting. Try this:
preg_replace('/<link>(.*)\|(.*)<\/link>/','<a href="$2?
sessid=0343erererere34343">$1</a>',$text);
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: unset multidimensional array element
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 20:21:16 GMT 2024

Total time taken to generate the page: 0.02116 seconds