Converting text to hyperlinks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Converting text to hyperlinks

Post by Pavilion »

Hello:

I am pulling standardized text urls from my databases to my website and want to create hyperlinks from the text values. All values are standardized as follows:

http://www.mysite.com

With all values standardized, in format, how can I convert them to hyperlinks?

Thanks Much - Pavilion
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Converting text to hyperlinks

Post by tr0gd0rr »

You'll need preg_replace. For your case, the regex could be as simple as '~^http\://\S+$~i'
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Converting text to hyperlinks

Post by Pavilion »

Code: Select all

You'll need preg_replace. For your case, the regex could be as simple as '~^http\://\S+$~i'
Yeah... I figured I'd need preg_replace. the problem is that I'm getting it wrong somewhere. Taking your suggestion, I changed my script to the following:

Code: Select all

$web_address = preg_replace('~^http\://\S+$~i','',$web_text);
When I echo out $web_address there is nothing.

I can echo out $web_text though. So I'm feeding preg_replace an actual value, just not executing it right. Any suggestions you have are welcome.

Thanks Much - Pavilion
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Converting text to hyperlinks

Post by Pavilion »

Well - this was an exercise of "making much ado about nothing" :lol:

Since my text web addresses are so standardized I was able to make them active hyperlinks as follows:

Code: Select all

$web_address = "<a href='" . $web_text ." '>" . substr($web_text,7) .  "</a>";
By running $web_text through a substr function, the users do not see the "http//" part of the address. They only see "www.mysite.com" as the visible hyperlink.

Someday I'll understand regex - but at this point it totally confuses me. (sigh).

Thanks for your help - Pavilion
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Converting text to hyperlinks

Post by TildeHash »

Pavilion wrote:Hello:

I am pulling standardized text urls from my databases to my website and want to create hyperlinks from the text values. All values are standardized as follows:

http://www.mysite.com

With all values standardized, in format, how can I convert them to hyperlinks?

Thanks Much - Pavilion
I know you solved your own problem already, but for future reference here is some code to change text URLs to hyperlinks:

Code: Select all

<?php
$textURL = 'http://www.example.com/test.html?query=value&some=thing';
$htmlURL = preg_replace('/(((ftp|http|https){1}:\/\/)[a-zA-Z0-9-@:%_\+.~#?&\/=]+)/i', '<a href="\\1" target="_blank">\\1</a>', $textURL);
echo $htmlURL;
?>
Post Reply