Note that there are some explanatory texts on larger screens.

plurals
  1. POFind URLs, @replies and #hashtags from Tweets
    text
    copied!<p>I'm building a little Twitter thing in PHP and I'm trying to parse URLs, @replies and #hashtags and make them into clickable links. </p> <ul> <li>The @replies would link to <a href="http://twitter.com/replies" rel="nofollow noreferrer">http://twitter.com/replies</a></li> <li><h1>Hashtags would like to <a href="http://search.twitter.com/search?q=%23hashtags" rel="nofollow noreferrer">http://search.twitter.com/search?q=%23hashtags</a></h1></li> </ul> <p>I've found a class for parsing URLs and I'm wondering if this could also be used to parse @replies and #hashtags as well:</p> <pre><code>// http://josephscott.org/archives/2008/11/makeitlink-detecting-urls-in-text-and-making-them-links/ class MakeItLink { protected function _link_www( $matches ) { $url = $matches[2]; $url = MakeItLink::cleanURL( $url ); if( empty( $url ) ) { return $matches[0]; } return "{$matches[1]}&lt;a href='{$url}'&gt;{$url}&lt;/a&gt;"; } public function cleanURL( $url ) { if( $url == '' ) { return $url; } $url = preg_replace( "|[^a-z0-9-~+_.?#=!&amp;;,/:%@$*'()x80-xff]|i", '', $url ); $url = str_replace( array( "%0d", "%0a" ), '', $url ); $url = str_replace( ";//", "://", $url ); /* If the URL doesn't appear to contain a scheme, we * presume it needs http:// appended (unless a relative * link starting with / or a php file). */ if( strpos( $url, ":" ) === false &amp;&amp; substr( $url, 0, 1 ) != "/" &amp;&amp; !preg_match( "|^[a-z0-9-]+?.php|i", $url ) ) { $url = "http://{$url}"; } // Replace ampersans and single quotes $url = preg_replace( "|&amp;([^#])(?![a-z]{2,8};)|", "&amp;#038;$1", $url ); $url = str_replace( "'", "&amp;#039;", $url ); return $url; } public function transform( $text ) { $text = " {$text}"; $text = preg_replace_callback( '#(?&lt;=[\s&gt;])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&amp;~/\-=?@\[\](+]|[.,;:](?![\s&lt;])|(?(1)\)(?![\s&lt;])|\)))*)#is', array( 'MakeItLink', '_link_www' ), $text ); $text = preg_replace( '#(&lt;a( [^&gt;]+?&gt;|&gt;))&lt;a [^&gt;]+?&gt;([^&gt;]+?)&lt;/a&gt;&lt;/a&gt;#i', "$1$3&lt;/a&gt;", $text ); $text = trim( $text ); return $text; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload