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
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
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
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
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.