コード:
$row['text'] = 'http://t.co/iBSiZZD4 and http://t.co/1rG3oNmc and http://t.co/HGFjwqHI and http://t.co/8UldEAVt';
if(preg_match_all('|http:\/\/t.co\/.{1,8}|i',$row['text'],$matches)){
foreach($matches[0] as $value){
$headers = get_headers($value,1);
if(is_array($headers['Location'])){
$headers['Location'] = $headers['Location'][0];
}
$row['text'] = preg_replace('|http:\/\/t.co\/.{1,8}|i', '<a href="' . $headers['Location'] . '">' . $headers['Location'] . '</a>',$row['text']);
}
}
これは に関連していget_headers()ます。場合によってget_headers($url,1)は、次のように位置インデックス キーを持つ配列を返します[Location]=>Array([0]=>url1 [1]=>url2)。私は基本的にif[Location]と同等にしたいです。ただし、上記のコードはそのタスクを達成していないようです。私も試してみましたが、どちらも問題を解決しませんでした。考え?[Location][0][Location][0]array_key_exists()isset()