나는이 정규식 #^https?://twitter\.com/(?:\#!/)?(\w+)/status(es)?/(\d+)$#is
정규식 : URL
있는 메시지에 발견하면, 트윗의 URL을 가져 오기 위해 노력하고있어에서 트윗 사용자 이름과 ID를 추출하지만 내 정규식이 트윗 URL에 대한 가져 오기 위해 정확하지 않은 것 같다 . 다음은 패턴이 처음부터 끝까지 일치해야 나타내는 정규 표현식에서 anchors을 포함하고 있기 때문에 예상대로이 작동하지 않을 이유는 내 전체 코드
function gettweet($string)
{
$regex = '#^https?://twitter\.com/(?:\#!/)?(\w+)/status(es)?/(\d+)$#is';
$string = preg_replace_callback($regex, function($matches) {
$user = $matches[2];
$statusid = $matches[3];
$url = "https://twitter.com/$user/status/$statusid";
$urlen = urlencode($url);
$getcon = file_get_contents("https://publish.twitter.com/oembed?url=$urlen");
$con = json_decode($getcon, true);
$tweet_html = $con["html"];
return $tweet_html;
}, $string);
return $string;
}
$message="This is absolutely trending can you also see it here https://twitter.com/itslifeme/status/765268556133064704 i like it";
$mes=gettweet($message);
echo $mes;
감사합니다. 정규식이 완벽하게 작동하지만이 구문을 분석 할 때 json의 응답이 없습니다. $ getcon = file_get_contents ("https://publish.twitter.com/oembed?url=$urlen"); $ con = json_decode ($ getcon, true); $ getva = $ con [ "url"]; –
응답이 없거나 'json_decode'가 null을 반환합니다. [매뉴얼에 따라] (http://php.net/json-decode)는 실패를 나타 냅니까? 또는'file_get_contents' 자체가'false'를 반환했는지 여부입니다. [매뉴얼에 따라] (http://php.net/file-get-contents)는 실패를 나타 냅니까? 코드에서 어떤 종류의 오류 처리도 시도하지 않습니다. 이 코드가 매번 완벽하게 작동해야한다고 생각할 때 코드가 예기치 않게 실패 할 수 있다는 것은 이상하거나 예기치 않은 현상이 아닙니다. – Sherif
답장을 보내 주셔서 감사합니다. 제발 PHP 초보자입니다. json_decode가 null을 반환하지만 구문 분석 된 트윗 URL이 유효합니다. 완벽한 결과를 얻으려면 최선을 다해 도와주세요. json 배열에서 'html'을 출력하고 내 웹 사이트에서 Embedded tweet로 표시하고 싶습니다. 감사 –