次のテキスト ブロックがあります。
$text = 'This just happened outside the store http://somedomain.com/2012/12/store there might be more text afterwards...';
次のように変換する必要があります。
$result['text_1'] = 'This just happened outside the store';
$result['text_2'] = 'there might be more text afterwards...';
$result['url'] = 'http://somedomain.com/2012/12/store';
これは私の現在のコードです。URL は検出されますが、テキストから削除することしかできません。配列内で URL 値を個別に取得する必要があります。
$string = preg_replace('/https?:\/\/[^\s"<>]+/', '', $text);
//returns "This just happened outside the store there might be more text afterwards..."
何か案は?ありがとう!
一時的な解決策(これを最適化できますか?)
$text = 'This just happened outside the store http://somedomain.com/2012/12/store There might be more text afterwards...';
preg_match('/https?:\/\/[^\s"<>]+/',$text,$url);
$string = preg_split('/https?:\/\/[^\s"<>]+/', $text);
$text = preg_replace('/\s\s+/','. ',implode(' ',$string));
echo '<a href="'.$url[0].'">'.$text.'</a>';