<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>¿Más código? &#187; preg_replace_callback</title>
	<atom:link href="http://www.mascodigo.com/tag/preg_replace_callback/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mascodigo.com</link>
	<description>Mantenlo simple, estúpido</description>
	<lastBuildDate>Wed, 14 Sep 2011 14:40:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Buscar enlaces en una cadena y convertirlos en enlaces HTML con php (preg_replace_callback).</title>
		<link>http://www.mascodigo.com/2009/12/10/buscar-enlaces-en-una-cadena-y-convertirlos-en-enlaces-html-con-php-preg_replace_callback/</link>
		<comments>http://www.mascodigo.com/2009/12/10/buscar-enlaces-en-una-cadena-y-convertirlos-en-enlaces-html-con-php-preg_replace_callback/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 02:00:57 +0000</pubDate>
		<dc:creator>Webmaster</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Enlaces]]></category>
		<category><![CDATA[parsear]]></category>
		<category><![CDATA[preg_replace_callback]]></category>

		<guid isPermaLink="false">http://www.mascodigo.com/?p=191</guid>
		<description><![CDATA[Actualmente casi todos los sitios webs tiene algún método para reconocer enlaces. Da igual donde dejemos un comentario. Puede ser un foro, un chat, un blog, etc. Seguro que cuando enviemos un mensaje, si este contiene algún enlace, una vez publicado podremos pinchar sobre él. Yo como usuario, lo encuentro bastante útil y cómodo. Por [...]]]></description>
			<content:encoded><![CDATA[<p>Actualmente casi todos los sitios webs tiene algún método para reconocer enlaces. Da igual donde dejemos un comentario. Puede ser un foro, un chat, un blog, etc. Seguro que cuando enviemos un mensaje, si este contiene algún enlace, una vez publicado podremos pinchar sobre él.</p>
<p>Yo como usuario, lo encuentro bastante útil y cómodo. Por un lado, si soy un visitante, es algo incomodo copiar y pegar el enlace en la barra de direcciones. Es mucho mas fácil hacer &#8220;click&#8221; sobre el enlace.</p>
<p>Por otro lado, si soy el autor de un comentario y quiero dejar un enlace, solo tengo que escribirlo, no tengo que preocuparme de hacer que sea pinchable.</p>
<p>Por ese motivo, dejo a vuestra disposición una clase en php para leer un texto y convertir en etiquetas HTML todos los enlaces que contenga el texto.</p>
<p>Actualmente solo reconoce los enlaces que comienzan por &#8220;http://&#8221; pero puede mejorarse para que detecte mas enlaces o realice mas operaciones, esto ya depende de las necesidades de cada uno.</p>
<p>A continuación el código y un ejemplo de uso.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Con esta clase podemos convertir los enlaces de un texto.
 * Por ejemplo si escribirmos:
 * http://www.ejemplo.com
 * se convertirá automáticamente en:
 * &lt;a href=&quot;http://www.ejemplo.com&quot;&gt;http://www.ejemplo.com&lt;/a&gt;
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Enlaces
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_texto</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_logs</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_debug</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_existen</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$debug</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//Saber si hay que mostar los mensajes de debug por pantalla o no.</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$debug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_debug <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Verificar que se indicó un texto a parsear</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$texto</span> <span style="color: #339933;">||</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_logs <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Error! Usted no indico la cadena a parsear. rn&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_texto <span style="color: #339933;">=</span> <span style="color: #000088;">$texto</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Parsear texto</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parsear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_logs <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Error! El texto indicado no se puede parsear o no
        tiene enlaces. rn&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_logs <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Texto parseado correctamente&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Parsear el texto buscando enlaces. Se buscan las cadenas que comienzan por:
 * &quot;http://&quot; sin comillas.
 *
 * Retorna: true si se parsea correctamente, false si no se parsea o
 * si no existen enlaces en el texto.
 */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> parsear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_texto <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/(http://[w/*?*&amp;*=*.*]+)/&quot;</span><span style="color: #339933;">,</span>
                             <span style="color: #0000ff;">&quot;Enlaces::enlacesDetectados&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_texto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_texto <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;NULL&quot;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_existen <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Convierte los enlaces en enlaces HTML. Si queremos añadir atributos a los enlaces generados, debemos
 * hacerlo aquí.
 */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enlacesDetectados<span style="color: #009900;">&#40;</span><span style="color: #000088;">$coincidencias</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$coincidencias</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$enlace</span><span style="color: #009900;">&#41;</span>
         <span style="color: #009900;">&#123;</span>
             <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_existen <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
             <span style="color: #000088;">$enlace</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;a href='&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$enlace</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$enlace</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$enlace</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Devuelve el texto con los enlaces convertidos.
 */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTexto<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_texto<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Devuelve el logs generado durante la ejecución. Solo devuelve el logs si se indico previamente.
 */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLogs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_debug<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_logs<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * EJEMPLO DE USO
 */</span>
 <span style="color: #000088;">$texto</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; Este es un texto de prueba. Con un enlace a http://www.google.es y
otro enlace a http://google.com, &amp;iquest; Que te parece ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$enlaces</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Enlaces<span style="color: #009900;">&#40;</span><span style="color: #000088;">$texto</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$enlaces</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTexto</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Se imprime:
 *
Este es un texto de prueba. Con un enlace a &lt;a href='http://www.google.es'&gt;http://www.google.es&lt;/a&gt; y
otro enlace a &lt;a href='http://google.com'&gt;http://google.com&lt;/a&gt;, &amp;iquest; Que te parece ?
&nbsp;
 */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.mascodigo.com/2009/12/10/buscar-enlaces-en-una-cadena-y-convertirlos-en-enlaces-html-con-php-preg_replace_callback/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

