Page 1 of 1
Converting text to hyperlinks
Posted: Wed Jul 25, 2012 1:19 pm
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
Re: Converting text to hyperlinks
Posted: Wed Jul 25, 2012 1:55 pm
by tr0gd0rr
You'll need
preg_replace. For your case, the regex could be as simple as '~^http\://\S+$~i'
Re: Converting text to hyperlinks
Posted: Wed Jul 25, 2012 2:14 pm
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
Re: Converting text to hyperlinks
Posted: Wed Jul 25, 2012 3:39 pm
by Pavilion
Well - this was an exercise of "making much ado about nothing"
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
Re: Converting text to hyperlinks
Posted: Sat Jul 28, 2012 2:17 am
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;
?>