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
https://forums.phpfreaks.com/topic/79067-solved-find-and-parse-url/
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

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.