Note that there are some explanatory texts on larger screens.

plurals
  1. POTurning XML Twitter data into clickable links using preg_replace
    primarykey
    data
    text
    <p>I'm pretty new to coding and want to try and build a twitter app for my website. With a little help I was able to get all the basics down, but what I'm now struggling with is two things:</p> <ol> <li>Turning the Twitter XML data into clickable links</li> <li>Making sure the XML text renders properly (It seems like "" (quotes) from the XML are being rendered improperly (as "“").</li> </ol> <p>I seem to have found the answers on how to fix these two things, but being so new to PHP, I'm not exactly sure how to implement these two fixes to my code. Any help would be greatly appreciated.</p> <p>Solution I found to problem 1:</p> <pre><code>function twitterify($status) { $status = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t&lt; ]*)#", "\\1&lt;a href=\"\\2\" target=\"_blank\"&gt;\\2&lt;/a&gt;", $status); $status = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r&lt; ]*)#", "\\1&lt;a href=\"http://\\2\" target=\"_blank\"&gt;\\2&lt;/a&gt;", $status); $status = preg_replace("/@(\w+)/", "&lt;a href=\"http://www.twitter.com/\\1\" target=\"_blank\"&gt;@\\1&lt;/a&gt;", $status); $status = preg_replace("/#(\w+)/", "&lt;a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\"&gt;#\\1&lt;/a&gt;", $status); return $status; } </code></pre> <p>Solution I found to problem 2 is to use html_entity_decode... but again I'm just not sure how to implement these two solutions into my code.</p> <p>My code so far: </p> <pre><code>&lt;?php $xmldata = 'https://twitter.com/statuses/user_timeline/carmeloanthony.xml'; $open = fopen($xmldata, 'r'); $content = stream_get_contents($open); fclose($open); $xml = new SimpleXMLElement($content); ?&gt; &lt;table class="table table-striped"&gt; &lt;?php foreach($xml-&gt;status as $status) { ?&gt; &lt;tr&gt; &lt;td&gt; &lt;img src=" &lt;?php echo $status-&gt;user-&gt;profile_image_url; ?&gt;" /&gt; &lt;/td&gt; &lt;td&gt;&lt;strong&gt; &lt;?php echo $status-&gt;user-&gt;name; ?&gt;&lt;/strong&gt; &lt;i&gt;@&lt;?php echo $status-&gt;user-&gt;screen_name; ?&gt;&lt;/i&gt; &lt;br /&gt; &lt;?php echo $status-&gt;text; ?&gt;&lt;/td&gt; &lt;td style="width: 40px;"&gt;&lt;?php echo date("M j",strtotime($status-&gt;created_at)); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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