0

So based on this post

I have the following code which almost works, with the exception that clean is returning an error

syntax error, unexpected '0' (T_LNUMBER)

$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$clean = str_replace(['https://', 'http://', 'www.'], '', $0);
$html = '<a href="$0" target="_blank" title="$0">'.$clean.'</a>';
$questionLinks = preg_replace($url, $html, $question->question);

The problem is with the $0, how can I have a normal variable to work with?

TrOnNe
  • 1,632
  • 16
  • 30
  • 1
    and what's the content of `$0`? – ADyson Mar 30 '18 at 19:30
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Nigel Ren Mar 30 '18 at 19:36
  • I'm not sure what is $0 apparently it returns the whole URL, but I can't find documentation. And sadly it's not a duplicate, I can't solve my question with that – TrOnNe Mar 30 '18 at 20:03

1 Answers1

1

try this:

$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
preg_match($url, $question->question, $completeUrl);
$clean = preg_replace('~^www.~', '', $completeUrl[2]);
$http = strpos($question->question, 'http');
$html = ($http ? '<a href="$0" target="_blank" title="$0">'.$clean.'</a>' : '<a href="http://$0" target="_blank" title="$0">$0</a>');
$questionLinks = preg_replace($url, $html, $question->question);