I have this block of text:
$text = 'This just happened outside the store http://somedomain.com/2012/12/store there might be more text afterwards...';
It needs to be converted to:
$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';
This is my current code, it does detect the url, but i can only remove it from the text, I still need the url value separately in an array:
$string = preg_replace('/https?:\/\/[^\s"<>]+/', '', $text);
//returns "This just happened outside the store there might be more text afterwards..."
Any ideas? Thanks!
Temporal solution (can this be optimized?)
$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>';
preg_match()andpreg_match_all()are for extracting. Do your replacement afterwards. (Or actually,preg_replace_callback()to do all at once.)