我有这段文字:
$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>';