I'm trying to make a script which changes all urls in $string to LINKS.
What I have is...
<?php
$string = 'Some text here
Google gesture search
http://www.youtube.com/watch?v=txX5rXtY7CY
Google Goggles
http://www.youtube.com/watch?v=Hhgfz0zPmH4
Google Skymap
http://www.youtube.com/watch?v=p6znyx0gjb4';
$regex = '((http|https|ftp)://([-a-zA-Z0-9?éÉýÝúÚíÍóÓðÐáÁæÆþÞ@\#-/&\\$%_=:\;\.~])*)';
$prexp = '#' . $regex . '#i';
$matches = preg_match_all( $prexp, $string, $output );
for( $i = 0; $i < $matches; $i++ )
{
if( strlen( $output[1][$i] ) > 70 )
$output[2][$i] = substr_replace( $output[1][$i], '...', 35, -35 );
else
$output[2][$i] = $output[1][$i];
$rpl = ereg_replace( $regex,'<a href="\1" target="_blank">' . $output[2][$i] . '</a>', $output[1][$i] );
$string = preg_replace( $prexp, $rpl, $string, 1);
}
echo nl2br( $string );
?>
The output is:
Some text here
Google gesture search
http://www.youtube.com/watch?v=p6znyx0gjb4" target="_blank">http://www.youtube.com/watch?v=Hhgfz0zPmH4" target="_blank">http://www.youtube.com/watch?v=txX5rXtY7CY
Google Goggles
http://www.youtube.com/watch?v=Hhgfz0zPmH4
Google Skymap
http://www.youtube.com/watch?v=p6znyx0gjb4
What is wrong?