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!
I'm building a site that is heavy on PHP and MySQL and I'm a self learner newcomer so I have some doubts, most I can work out myself others I can search the web for answers with a fair amount of success, others like this one I don't even know how to look for it
My question is this:
There will be inside a field on a table some entries in the middle of a text like <http://www.somewhere.com Somewhere you should go> and I want php to resolve this text and output something like <a href="http://www.somewhere.com">Somewhere you should go</a>. Of course this text is only is only a stand in and there could be a lot of this kind of entries so it would need to be fully automatic and search the entire text for them.
paulopocas wrote:<http://www.somewhere.com Somewhere you should go> and I want php to resolve this text and output something like <a href="http://www.somewhere.com">Somewhere you should go</a>.
$text='<http://www.somewhere.com Somewhere you should go>';
$new=preg_replace('/\\<((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))\\s(.*?)\\>/is','<a href="$1">$2</a>',$text);
echo $new //<a href="http://www.somewhere.com">Somewhere you should go</a>
If I were you, I would use square brackets instead of greater, and less than signs. Keep it seperate from XML style tags if possible. If you want to do that, use:
$text='[http://www.somewhere.com Somewhere you should go]';
$new=preg_replace('/\\[((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))\\s(.*?)\\]/is','<a href="$1">$2</a>',$text);
echo $new //<a href="http://www.somewhere.com">Somewhere you should go</a>