2010-11-27 3 views
2

J'ai bricolé le bloc de code ci-dessous pour m'aider à afficher le dernier tweet sur mon site depuis mon compte Twitter. Cependant, ça ne marche pas très bien, pouvez-vous m'aider à déboguer ce dernier petit bout. Je suis à la recherche de PHP pour le transformer en HTML avec des balises de lien enroulées autour des noms d'utilisateur Twitter et des liens qu'il utilise en utilisant preg_replace.Transformez les URL en liens en affichant le dernier tweet d'un compte Twitter avec preg_replace

Si vous testez ce script, vous verrez qu'il y a un problème avec quand il rend des liens standards dans les tweets il met la balise de fermeture < trop tôt après a. Je suis sûr que c'est relativement simple à corriger et probablement à propos d'échapper des caractères ou quelque chose.

Mon principal codeblock:

<?php 
     /** Script to pull in the latest tweet */ 
     $username='benpaton'; 
     $format = 'json'; 
     $tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); 
     $latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES); 
     $latestTweet = preg_replace('/http:\/\/([[a-z0-9_\.\-\+\&\!\#\~\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet); 
     $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet); 
     echo $latestTweet; 
    ?> 
+0

Pensée: pourriez-vous diviser le tweet par des espaces et convertir à l'URL l'un de ces segments qui commencent par « http: // »? C'est beaucoup, beaucoup moins cher que d'exécuter une expression régulière, plus simple, et je pense que cela fonctionnerait aussi bien pour ce cas d'utilisation. – Matchu

+0

Cela semble faisable. Je ne suis pas sûr comment faire cela bien que les @usernames ne commencent pas par "http: //" –

Répondre

6

Changer votre expression régulière:

$latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet); 

Cela a fonctionné pour moi.

code complet

<?php 
    /** Script to pull in the latest tweet */ 
    $username='benpaton'; 
    $format = 'json'; 
    $tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); 
    $latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES); 
    $latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet); 
    $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet); 
    echo $latestTweet; 
?> 
+0

Quand j'utilise votre ligne preg_replace insérée dans mon bloc de code $ latestTweet ne reçoit pas d'écho du tout pour moi qui c'est bizarre. –

+0

il affiche quelque chose comme ça @bandq @Yellowkim vérifier ceci: <- http://paper.li/bandq -> (lien complet) –

+0

vous pouvez tester en allant ici - http: //jawilliams.site11 .com/tweet.php –

0

Essayez ceci:

<?php 
/** Script to pull in the latest tweet */ 
$username='benpaton'; 
$format = 'json'; 
$tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); 
$latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES); 
$latestTweet = preg_replace('%http://[a-z0-9_.+&!#~/,\-]+%', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet); 
$latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet); 
echo $latestTweet; 
?> 
0

Tout le code est bogué ou incomplet! Qu'est-ce que vous voulez faire quelque chose comme:

$tweets[$i]['text_html'] = htmlspecialchars($tweet['text']); 
$tweets[$i]['text_html'] = preg_replace('%(http://([a-z0-9_.+&!#~/,\-]+))%i','<a href="http://$2">$1</a>',$tweets[$i]['text_html']); 
$tweets[$i]['text_html'] = preg_replace('/@([a-z0-9_]+)/i','<a href="http://twitter.com/$1">@$1</a>',$tweets[$i]['text_html']); 
Questions connexes