I have a slight issue that I am in need of help with.
Say if $post['text'] was equal to data like: "https://stackoverflow.com/blah?blah is such a cool website", how would it be possible to make just the URL bit in a type tag...
So the end HTML result would be somthing like
<p><a href="https://stackoverflow.com/blah?blah">https://stackoverflow.com/blah?blah</a> is such a cool website</p>
I am a bit of a noob. I'm actually a 14 year old kid and still kinda new to code, but I loveee it!
You can use regular expersion.
function make_hyperlink($text)
{
// match protocol://address/path/
$text = preg_replace("{[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*}", "<a href=\"\\0\" target='_blank'>\\0</a>", $text);
// match www.something
$text = preg_replace("{(^| )(www([.]?[a-zA-Z0-9_/-])*)}", "\\1<a href=\"http://\\2\" target='_blank'>\\2</a>", $text);
// return $text
return $text;
}
echo make_hyperlink('http://stackoverflow.com/blah?blah');
Use above function. It will replace URL from a simple url text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With