Jump to content

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


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 :)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.