2014-01-08 79 views
-1

林之間提取文本試圖從Instagram的API評論數據,例如 網址:https://api.instagram.com/v1/media/375163873469222039_180213154/comments?access_token=180213154.f59def8.f888fe332f7c47e98bd20a44866ef0be從Instagram的API獲取JSON數據和特定的關鍵字

,我要的是得到給出了具體的關鍵字之間的文本。所以,從示例URL我想要在雙引號和標籤#title之間獲取文本。所以結果是 - >壽司自制

如何通過PHP來做到這一點?任何幫助表示讚賞。

+0

+0

以及如何從指定的關鍵字提取文本之間的json? – cutez7boyz

+0

使用json解碼和解析 –

回答

2

試試這個簡單的方法

<?php 
$json = '{"meta":{"code":200},"data":[{"created_time":"1358953338","text":"ciye koki sushi :))","from":{"username":"mayarahmanita","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_282786052_75sq_1357394407.jpg","id":"282786052","full_name":"Mayaa Rahmanita Hardianti"},"id":"375250258876255756"},{"created_time":"1358954810","text":"baguuus kan ada yg digoreng jg lho :-D","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"375262607376994013"},{"created_time":"1358960360","text":"mau dong nyoba :3","from":{"username":"mayarahmanita","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_282786052_75sq_1357394407.jpg","id":"282786052","full_name":"Mayaa Rahmanita Hardianti"},"id":"375309164881202914"},{"created_time":"1358960656","text":"makanya jgn lama2 di balinya :-[","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"375311649997934355"},{"created_time":"1388808291","text":"Sushi Homemade #title","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"625691760184610726"},{"created_time":"1388808305","text":"Rp50.000 #price","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"625691875997732786"}]}'; 

$arr = json_decode($json); 

if(is_array($arr->data)) {  
    foreach($arr->data as $key=>$val){ 
     if(preg_match('/#title/', $val->text)){ 
      print preg_replace('/#title/', '', $val->text); 
      break; 
     } 
    } 
} 
+0

謝謝,正是我需要的! – cutez7boyz

+0

歡迎您..享受編碼.. :-) – kwelsan

+0

你好kwelsan,如果我想添加更高級的過濾器,只有當用戶名:realwahyuputra才能獲得價值。因此,如果用戶名的值是realwahyuputra,那麼只會提取#title和doublequote之間的值。你能幫我:) – cutez7boyz

2

試試這個,

<?php 
$url  = "https://api.instagram.com/v1/media/375163873469222039_180213154/comments?access_token=180213154.f59def8.f888fe332f7c47e98bd20a44866ef0be"; 
$response = json_decode(file_get_contents($url)); 
echo "<pre>"; 
foreach($response->data as $res) { 
echo "Title".$res->text."\n"; 
} 
?> 

結果:

Titleciye koki sushi :)) 
Titlebaguuus kan ada yg digoreng jg lho :-D 
Titlemau dong nyoba :3 
Titlemakanya jgn lama2 di balinya :-[ 
TitleSushi Homemade #title 
TitleRp50.000 #price 
+0

謝謝我將你的答案與另一個結合起來,以適應我想要的。 <?php $ json =「https://api.instagram.com/v1/media/375163873469222039_180213154/comments?access_token=180213154.f59def8.f888fe332f7c47e98bd20a44866ef0be」; $ arr = json_decode(file_get_contents($ json));如果(preg_match('/#price /',$ val-> text)){ foreg_replace('/#price /','' ,$ val-> text); 休息; } } – cutez7boyz

1

這是一個有點長切因爲我不熟悉與正則表達式,所以我已經使用字符串函數。試用於不同的場合,看它是否行之有效?

$str = '{"meta":{"code":200},"data":[{"created_time":"1358953338","text":"ciye koki sushi :))","from":{"username":"mayarahmanita","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_282786052_75sq_1357394407.jpg","id":"282786052","full_name":"Mayaa Rahmanita Hardianti"},"id":"375250258876255756"},{"created_time":"1358954810","text":"baguuus kan ada yg digoreng jg lho :-D","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"375262607376994013"},{"created_time":"1358960360","text":"mau dong nyoba :3","from":{"username":"mayarahmanita","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_282786052_75sq_1357394407.jpg","id":"282786052","full_name":"Mayaa Rahmanita Hardianti"},"id":"375309164881202914"},{"created_time":"1358960656","text":"makanya jgn lama2 di balinya :-[","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"375311649997934355"},{"created_time":"1388808291","text":"Sushi Homemade #title","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"625691760184610726"},{"created_time":"1388808305","text":"Rp50.000 #price","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":""},"id":"625691875997732786"}]}'; 

$substring = substr($str,0,strpos($str,'#title')); //returns from start of string till Sushi Homemade 
$new2 = strrpos($substring,'"',-1); //searches backwards and returns position of first " found 
$new3 = substr($substring,$new2+1,strlen($substring)); //extracts string from " till end of string 
echo trim($new3); //trimming forward and trailing space and displaying the needed string value