
- You are not logged in. | Login
February 28, 2007 9:05 am
- jjjlc1983
- Member


URL-handling within a string
Hi! I have such a question:
There is a string
New session. Referer: http://blablabla.com/43534/234234 to https://blibli.com
I need to replace all the URLs found in the string (independent from which URL it is – http, https, ftp and so on) with links i.e. I’d like to get something like this:
New session. Referer: <A href="http://blablabla.com/43534/234234">blablabla.com</A> to <A href="https://blibli.com ">blibli.com </A>
Many thanks in regard to your answers!
February 28, 2007 9:14 am
- jjjlc1983
- Member


Re: URL-handling within a string
Have you tried to manage it yourself?
Sure…
IF(strpos($row['action'], "http://"))
{
$pma = strpos($row['action'], "http://");
preg_match("/^(http:\/\/)?([^\/]+)/i", substr($row['action'], $pma), $host);
$pma = substr($row['action'], 0, $pma).'<A href="'.substr($row['action'], $pma).'" target="_blank">'.$host[2].'</A>';
}
ELSE IF(strpos($row['action'], "https://"))
{
$pma = strpos($row['action'], "https://");
preg_match("/^(https:\/\/)?([^\/]+)/i", substr($row['action'], $pma), $host);
$pma = substr($row['action'], 0, $pma).'<A href="'.substr($row['action'], $pma).'" target="_blank">'.$host[2].'</A>';
}
ELSE
{
$pma = $row['action'];
}But I’d like something more rational! And it should include handling of all the links not only one and only in case when a link is in the end of a string!
February 28, 2007 9:17 am
- monkeydude
- Member


Re: URL-handling within a string
Hi! The two fragments in this code differ with one letter, don’t they?
February 28, 2007 9:21 am
- jjjlc1983
- Member


Re: URL-handling within a string
The two fragments in this code differ with one letter, don’t they?
Your guess is quite right.
I understand that it isn’t good but this is a raw code…I’ll complete it for sure… but nevertheless I’m interested in the question from the first post!
February 28, 2007 9:28 am
- ABK
- Member


Re: URL-handling within a string
Search+Manuals. As a rule, there are a lot of examples in the manuals in which you can always find something useful.
February 28, 2007 9:36 am
- shadow122
- Member


Re: URL-handling within a string
Look…what do you need this all for? One preg_replace is enough. You are left only to write an expression itself… for example, for http/https you can do like this:
echo preg_replace('!(https?)://([^ /]+)([^ ]*)!', '<a href="\1://\2\3">\2</a>', 'New session. Referer: http://blablabla.com /43534/234234 to https://blibli.com ');
February 28, 2007 9:52 am
- jjjlc1983
- Member


Re: URL-handling within a string
Look…what do you need this all for? One preg_replace is enough. You are left only to write an expression itself… for example, for http/https you can do like this:
echo preg_replace('!(https?)://([^ /]+)([^ ]*)!', '<a href="\1://\2\3">\2</a>', 'New session. Referer: http://blablabla.com /43534/234234 to https://blibli.com ');
Thanks a lot!


