You may have read my earlier thread using some regex to make http://www.whatever.com/ into a link. I need to make it so it does a substr on the link so it will link to the entire link but only show up to about 30 characters.
Turn http://www.google.com/blah12345678910
Into
<a href="http://www.google.com/blah12345678910">http://www.google.com/blah123</a>
I tried doing this:
$replacement = "\\1<a href=\"\\2\" target=\"_blank\">".substr("\\2", 0, 30)."</a>";
But it completely ignores the substr
Try doing something like this:
$replacement = "'\\1<a href=\"\\2\" target=\"_blank\">'.substr(\"\\2\", 0, 30).'</a>'";
Then, on your matching mattern, add an 'e' modifier after the last delimiter.
What do you mean by 'e'?
//tried this $pattern = '([^"\'])(http[s]?://[[a-zA-Z0-9_-]+\.]?[a-zA-Z0-9_/?&%-]+\.[a-zA-Z0-9_-]+/?[[a-zA-Z0-9+_:;=/?&%\.-]+]?)\e';
$pattern = '/([^"\'])(http[s]?:\/\/[[a-zA-Z0-9_-]+\.]?[a-zA-Z0-9_\/?&%-]+\.[a-zA-Z0-9_-]+\/?[[a-zA-Z0-9+_:;=\/?&%\.-]+]?)/e'; $text = preg_replace($pattern, "'\\1<a href=\"\\2\">'.substr('\\2', 0, 60).'</a>'" , $text);
🙂