Friday 9 December 2016

php - Syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'



I'm getting Syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'



This is the code i'm using



function wpse44503_filter_content( $content ) {
$regex = '#src=("|\')'.
'(/images/(19|20)(0-9){2}/(0|1)(0-9)/[^.]+\.(jpg|png|gif|bmp|jpeg))'.

'("|\')#';
$replace = 'src="'.get_site_url( $2 ).'"';

$output = preg_replace( $regex, $replace, $content );

return $output;
}


This is the line where i'm getting that error $replace = 'src="'.get_site_url( $2 ).'"';




Can anyone help me to fix it?
Thanks


Answer



What you're trying to do (ie replacing the matched string with the result of a function call) can't be done using preg_replace, you'll need to use preg_replace_callback instead to get a function called for every match.



A short example of preg_replace_callback;



$get_site_url =                    // Returns replacement
function($row) {

return '!'.$row[1].'!'; // row[1] is first "backref"
};

$str = 'olle';
$regex = '/(ll)/'; // String to match

$output = preg_replace_callback( // Match, calling get_site_url for replacement
$regex,
$get_site_url,
$str);


var_dump($output); // output "o!ll!e"

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...