Jump to content

make url html link


jaymc

Recommended Posts

Rather than make my own function Im just wondering if there is a predefined one or one you guys know of

 

I want to have something that will turn a link someone has entered (www.website.com) and wrap it in html to make it a link so:

 

<a href="http://www.website.com" target="blank">www.website.com</a>

Link to comment
https://forums.phpfreaks.com/topic/76538-make-url-html-link/
Share on other sites

yes, kinda

 

use regex and say

replace anything that matches anything.www.anything.anythinghere with

<a href=\"".$match."\" target=\"_blank"\">".$match."</a>";

 

you will need to discovery the matches and apply the complex replacement in a two fold step, but get your regex pattern built first.

Link to comment
https://forums.phpfreaks.com/topic/76538-make-url-html-link/#findComment-387627
Share on other sites

We don't know how you are accepting your input. I'll leave that problem for you... just remember, garbage in, garbage out. So let's say you have an input that has satisfied your script, and we now have it in a variable. You would do the following:

 

<?php
$weblink = 'www.somesite.com'; // this is the final result of your script processing the user input
$weblink = '<a href="http://' . $weblink . '" target="blank">' . $weblink . '</a>';
echo $weblink;
?>

 

PhREEEk

Link to comment
https://forums.phpfreaks.com/topic/76538-make-url-html-link/#findComment-387650
Share on other sites

Well its a substr of a large string so you are going to need to find said substr.  Which is going to need to be found using a regex to find all the matches.  Its a complicated thing, but can be done for presentation purposes, I wouldn't be surprised if one exist already

Link to comment
https://forums.phpfreaks.com/topic/76538-make-url-html-link/#findComment-387655
Share on other sites

Yes I see, lets say the string is this

 

 

Hey my website is www.website.com go and visit it please, thanks

 

How are we going to pick out www.website.com

 

I suppose you could explode the string by " ", then search through each elements of the array for "www." for instance, if it meets it, do the a href stuff

 

Its a bit long winded though to do that, any better ideas?

 

The regex seems good but its extreemly messy, I've never managed to get my head around it. Any examples?

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/76538-make-url-html-link/#findComment-388003
Share on other sites

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.