Jump to content

Search text for links and make them to <a href>'s


seksislav

Recommended Posts

Hello guys,

 

I search the forum a bit, but didnt find anything. I have a text and somewhere in the text are some http://www.link.com and http://link.com links, but as text only, so I need to replace them with html a tags. Could anybody help me a bit with the expression. Having a hard time. Thanks in advance.

 

 

Depends how substantial you require it to be. Are all your links going to be http?  Here's an example but it is very simple. If there are specific url's you need to include or you need to validate them as proper links then you will need to let us know.

 

echo preg_replace("#(https?://[^ ]+)#", '<a href="$1">$1</a>', $input);

No need to capture the match, as the full pattern match is stored in $0. And I would use \S instead of [^ ], to stop the matching at any whitespace character (most importantly a line break). And probably add the i modifier :)

Like I said, the example given was about the most basic it could be (bar perhaps removing the optional s for https). :) Your right I could have used $0, I was actually planning on making the pattern more complicated which would have required the capture group, but then I thought, fuck it, this meets the stated objectives and I need to be at my pool match in half hour.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.