seksislav Posted November 24, 2009 Share Posted November 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182816-search-text-for-links-and-make-them-to-s/ Share on other sites More sharing options...
cags Posted November 24, 2009 Share Posted November 24, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/182816-search-text-for-links-and-make-them-to-s/#findComment-964907 Share on other sites More sharing options...
seksislav Posted November 24, 2009 Author Share Posted November 24, 2009 Its ok like this. Thanks mate, works like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/182816-search-text-for-links-and-make-them-to-s/#findComment-964931 Share on other sites More sharing options...
thebadbad Posted November 24, 2009 Share Posted November 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182816-search-text-for-links-and-make-them-to-s/#findComment-964962 Share on other sites More sharing options...
cags Posted November 24, 2009 Share Posted November 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182816-search-text-for-links-and-make-them-to-s/#findComment-965103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.