![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
30/09/2005, 16:58
|
| | Fecha de Ingreso: febrero-2004
Mensajes: 9
Antigüedad: 21 años Puntos: 0 | |
Yo tengo el mismo problema y he hecho un código php recorriendo todo el texto.
1)todos los www los paso a http, y así tengo todos los url de la misma forma, y luego 2)por cada url le añado <a href=....>url </a>.
Pero el problema es k recorro dos veces el texto y eso es costoso. Además como busco urls que acaben con espacio si acaba con return tengo problemas. Me podríais decir una forma más fácil sin obligar al usuario a utilizar bbcode?
/////////////////////// CODIGO PHP /////////////////////
function reconocer_url($texto){
$offset=1;
$start=true;
$inici="www";
$texto=' '.$texto.' ';
while($start){
$start = strpos($texto, $inici,$offset);
if($start!=false){
$offset_plain=$start-$offset;//Obtenemos la longitud de texto normal
$offset_normal=$offset;//inicio del texto normal
$end = strpos($texto, ' ',$start);
$offset=$end+1;//Offset siguiente bucle
$end=$end-$start;
$url1=substr($texto,$start,$end);
//echo "start $start";
//echo "end $end";
//echo "url1 $url1";
$posic=$start-7;
if($posic >= 0){
$caracter= strpos($texto,"http://",$posic);
//echo "posicion http $caracter";
//echo "start $start";
if($caracter==$start-7){
$url2=substr($texto,$start,$end).' ';
}
else{
$url2="http://".substr($texto,$start,$end).' ';
}
}
else{
$url2="http://".substr($texto,$start,$end).' ';
}
//str_replace($url1, $url2,$start);
//echo "url2 $url2";
$result=$result.substr($texto,$offset_normal,$offs et_plain).$url2;
}
}
if(offset < strlen($texto)){
$result=$result.substr($texto,$offset,strlen($text o)-offset);}
//echo "result1 es $result";
$inici="http";
$start=true;
$offset=1;
$texto=' '.$result.' ';
$result=' ';
while($start)
{
$start = strpos($texto, $inici,$offset);
//echo "start $start";
if($start!=false){
$offset_plain=$start-$offset;//Obtenemos la longitud de texto normal
$offset_normal=$offset;//inicio del texto normal
$end = strpos($texto, ' ',$start);
$offset=$end+1;//Offset siguiente bucle
$end=$end-$start;
$url=substr($texto,$start,$end);
//echo "url $url";
$link='<a href='.$url.'>'.$url.' '.'</a>';
//str_replace($url, $link,$start);
//echo "url $link";
$result=$result.substr($texto,$offset_normal,$offs et_plain).$link;
}
}
if(offset < strlen($texto)){
$result=$result.substr($texto,$offset,strlen($text o)-offset);}
return $result;
}
//////////////////////////////////////////////////////////////////////////////////////////// |