Foros del Web » Programando para Internet » PHP »

Problema Expresion regular PHP

Estas en el tema de Problema Expresion regular PHP en el foro de PHP en Foros del Web. Hola a todos Estoy armando un funcion para reemplazar las url del youtbe y colocar el video atravez de un bbcode para esto estoy usando ...
  #1 (permalink)  
Antiguo 07/05/2010, 02:00
 
Fecha de Ingreso: abril-2008
Mensajes: 2
Antigüedad: 16 años
Puntos: 0
Exclamación Problema Expresion regular PHP

Hola a todos


Estoy armando un funcion para reemplazar las url del youtbe y colocar el video atravez de un bbcode

para esto estoy usando una expresion regular pero en ocaciones no funcione como deberia,

la expresion es la siguiente

Código PHP:
$ret=eregi_replace("<a(.*)href=\"http:\/\/www\.youtube\.com\/watch\?v=([\w]{11})(.*)\"(.*)>(.*)</a>","[youtube]\\2[/youtube]",$ret); 

esto debe extraer el codigo de la url ( en rojo lo que resuelve)

<a id="video-long-title-2BbkWHWXeWA" href="http://www.youtube.com/watch?v=2BbkWHWXeWA" class="yt-uix-hovercard-target" title="TU ME DAS CREMITA YO TE DOY CREMITA....... ..." rel="nofollow"><b>TU ME DAS CREMITA</b> YO TE DOY <b>CREMITA</b>....... <b>...</b></a>

<a id="video-long-title-2BbkWHWXeWA" href="http://www.youtube.com/watch?v=Eklz3Q8HDiA" class="yt-uix-hovercard-target" title="TU ME DAS CREMITA YO TE DOY CREMITA....... ..." rel="nofollow"><b>TU ME DAS CREMITA</b> YO TE DOY <b>CREMITA</b>....... <b>...</b></a>

<a id="video-long-title-2BbkWHWXeWA" href="http://www.youtube.com/watch?v=FBJqvj4fL60" class="yt-uix-hovercard-target" title="TU ME DAS CREMITA YO TE DOY CREMITA....... ..." rel="nofollow"><b>TU ME DAS CREMITA</b> YO TE DOY <b>CREMITA</b>....... <b>...</b></a>


en estos caso es donde no funciona correctamente


<a id="video-long-title-ReqIkP9eOs0" href="http://www.youtube.com/watch?v=2XFEKRKJsgE&feature=related" class="yt-uix-hovercard-target" title="Tú me das cremita yo te doy..." rel="nofollow"><b>Tú me das cremita</b> yo te doy...</a>


<a id="video-long-title-dXxiC7ck5Io" href="http://www.youtube.com/watch?v=cS0O-o9qY9I&feature=related" class="yt-uix-hovercard-target" title="BUENAFUENTE 384 - Entrevista con Wyoming" rel="nofollow"><b>BUENAFUENTE</b> 384 - Entrevista con Wyoming</a>

y en estos casos no resuelve nada

desde ya muchas gracias
  #2 (permalink)  
Antiguo 07/05/2010, 04:13
Avatar de Fernand0  
Fecha de Ingreso: septiembre-2005
Ubicación: Buenos Aires
Mensajes: 610
Antigüedad: 18 años, 7 meses
Puntos: 19
Respuesta: Problema Expresion regular PHP

Código PHP:
Ver original
  1. $ret='<a id="video-long-title-2BbkWHWXeWA" href="http://www.youtube.com/watch?v=2BbkWHWXeWA" class="yt-uix-hovercard-target" title="TU ME DAS CREMITA YO TE DOY CREMITA....... ..." rel="nofollow"><b>TU ME DAS CREMITA</b> YO TE DOY <b>CREMITA</b>....... <b>...</b></a>';
  2.  
  3. $ret2='<a id="video-long-title-dXxiC7ck5Io" href="http://www.youtube.com/watch?v=cS0O-o9qY9I&feature=related" class="yt-uix-hovercard-target" title="BUENAFUENTE 384 - Entrevista con Wyoming" rel="nofollow"><b>BUENAFUENTE</b> 384 - Entrevista con Wyoming</a>';
  4.  
  5. $pattern='#<a (.*) href=\"http:\/\/www\.youtube\.com\/watch\?v=([^&|\"]+)(.*)\"(.*)>(.*)</a>#';
  6. $replace='[youtube]$2[/youtube]';
  7.  
  8. $ret=preg_replace($pattern, $replace, $ret);
  9. $ret2=preg_replace($pattern, $replace, $ret2);
  10.  
  11. echo($ret);
  12. echo($ret2);

como odio las expresiones regulares.. voy a necesitar terapia ahora :x
  #3 (permalink)  
Antiguo 07/05/2010, 07:37
Colaborador
 
Fecha de Ingreso: octubre-2009
Ubicación: Tokyo - Japan !
Mensajes: 3.867
Antigüedad: 14 años, 6 meses
Puntos: 334
Respuesta: Problema Expresion regular PHP

pero si son geniales las expresiones regulares =)

aca te dejo una que probe recien

Código PHP:
Ver original
  1. $pattern = "@<a.*?href=[\"']http://www\.youtube\.com/watch\?v=(.*?)[&\"']@is";

recuerda usar expresiones compatibles con pearl ya que eregi y su familia estan obsoletas..

lee acerca de PCRE

eregi_replace se reemplazaria por preg_replace

saludos
__________________
More about me...
~ @rhyudek1
~ Github
  #4 (permalink)  
Antiguo 07/05/2010, 09:39
 
Fecha de Ingreso: abril-2008
Mensajes: 2
Antigüedad: 16 años
Puntos: 0
Respuesta: Problema Expresion regular PHP

Gracias por la ayuda, voy a probar y te cuento algo

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:42.