2011-12-06 2 views
-2

내 정규식 구문은 <> "rel ="행을 어쨌든 연결합니다. 여기twitter regex breaking, thinking syntax issue

그것이 :

Great deal: Jot by Adonit, a precise capacitive touch stylus, today 15% off with coupon code: 'Jot' - 
<a rel="\"nofollow\"" title="\"http://t.co/QvFi6CKK\"" href="\"http://t.co/QvFi6CKK\"">[link]</a> 

해시 태그는 HTML 구문 분석 :

I'm so sorry - that last # 
<a nofollow"="" href="http://search.twitter.com/search?q=%23GameOfShadowsUK? rel=">GameOfShadowsUK</a> 
tweet should hav 3been sent at 2:21 - my f****d up arsing w**k-mess of a life disallowed it :-( 

비닝 맛없어 코드를 더 나은 방법으로 갔다

<?php 
function parseTweet($text) { 
    $pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)[email protected])?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i'; 
    '@([A-Za-z0-9_]+)'; 

    $tweet = preg_replace('/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2? rel="nofollow">\2</a>', $text); 
    $tweet = preg_replace('/(^|\s)@(\w+)/', '\[email protected]<a href="http://www.twitter.com/\2? rel="nofollow">\2</a>', $tweet); 
    $tweet = preg_replace('#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,[email protected]\[\]+]*)(/[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)?)#is', '\\1 
         <a href=\"\\2\" title=\"\\2\" rel=\"nofollow\">[link]</a>', $tweet); 
    return $tweet; 
} 

$username='stephenfry'; // set user name 
$format='json'; // set format 
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable 

$theTweet = parseTweet($tweet[0]->text); 

echo $theTweet; 
?> 

링크는 HTML 구문 분석. 답변을 참조하십시오.

+1

일부 입력/출력 대 예상 출력을 게시 할 수 있습니까? –

+0

확실한 트윗 예제 출력을 게시했습니다. – SMacFadyen

+0

정규식 코드에서 식별 할 수없는 구문 문제가 있음을 알 수 있습니다. ""등 – SMacFadyen

답변

0
  <?php 

      function getLastXTwitterStatus($userid,$x){ 
      $url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x"; 

      $xml = simplexml_load_file($url) or die('could not connect'); 
       echo '<ul>'; 
        foreach($xml->status as $status){ 
        $text = twitterify($status->text); 
        echo '<li>'.utf8_decode($text).'</li>'; 
        } 
       echo '</ul>'; 
      } 

      function twitterify($ret) { 
       $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret); 
       $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret); 
       $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret); 
       $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret); 
      return $ret; 
      } 

      //my user id kenrick1991 
      getLastXTwitterStatus('simonpegg',1); 

      ?>