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
Converting text to hyperlinks
Moderator: General Moderators
Re: Converting text to hyperlinks
You'll need preg_replace. For your case, the regex could be as simple as '~^http\://\S+$~i'
Re: Converting text to hyperlinks
Code: Select all
You'll need preg_replace. For your case, the regex could be as simple as '~^http\://\S+$~i'Code: Select all
$web_address = preg_replace('~^http\://\S+$~i','',$web_text);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
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:
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
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>";Someday I'll understand regex - but at this point it totally confuses me. (sigh).
Thanks for your help - Pavilion
- TildeHash
- Forum Commoner
- Posts: 43
- Joined: Fri Jul 16, 2010 7:17 am
- Location: Apple Valley, California
Re: Converting text to hyperlinks
I know you solved your own problem already, but for future reference here is some code to change text URLs to hyperlinks: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
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;
?>