Jump to content

[SOLVED] Find and Parse URL


jaymc

Recommended Posts

I need to turn this

 

$mystring "This can be any txt at all but may contain a url like www.site.com of which needs parsing"

 

into

 

This can be any txt at all but may contain a url like <a href="http://www.site.com target=BlanK>www.site.com</a> of which needs parsing

 

I found the below php code in this section, but it only detects a urls presence within the string, doesnt actually do anything with it

 

if (preg_match('~
	(?:[a-z0-9+.-]+://)?  # A optional resource (http, ftp, etc)
	(?:\w+\.)+            # the host.domain
	\w{2,6}               # the TLD
	\S*                   # an optional path/to/file
	~ix', $mystring)) {
echo "no links, you turkey";
} else {
echo "nice comment";
}

 

If someone can help me with this I will be really happy! Driving me nuts!

 

If you want to go the extra mile, I need it in javascript aswell, but its more important php method works before anything else

Link to comment
Share on other sites

Doesnt output anything, if I take the link out, it prints the text fine

 

Obviously a glitch in code

 

Here, best to use live example and modify until working

 

$str = "dss s sd s www.site.com but may not work";
$str = preg_replace('/(((ftp|http|https):\/\/)?www\.(.*)+\.(.*)+) /','<a href="$1">$1</a>',$str);
echo $str;

 

Hope someone can help

Link to comment
Share on other sites

Er, looks like I put a stray www in there. Change to:

 

$str = preg_replace('/\b((?:[a-z0-9+.-]+:\/\/)?(?:\w+\.)+\w{2,6}\S*)/','<a href="$1">$1</a>',$str);

 

 

 

 

Tried to edit above post but the edit button has disappeared...

Link to comment
Share on other sites

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.