4
votes

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

5
$2 is an invalid PHP variable name.... "A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores."Mark Baker
Are you trying to pass the second capture group $2 into that function? (Interesting how many people seem to have missed this.)BoltClock♦
@BoltClock Yes thats right. I'm trying to pass the second capture group into that function. What should i put there instead of $2 ? ThanksGiri
What capture group? You haven't done a regex match (in the code that I can see) before the line $replace = 'src="'.get_site_url( $2 ).'"';.user554546
@user1091558 I added an example of preg_replace_callback to my answer below, you'll need to use that function to be able to call functions on a match.Joachim Isaksson

5 Answers

3
votes

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"
8
votes

You can't have '$2' as a variable name. It must start with a letter or underscore.

http://php.net/manual/en/language.variables.basics.php

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Edit Above was my original answer and is the correct answer to the simple "syntax error" question. More in-depth answer below...

You are trying to use $2 to represent "the second capture group", but you haven't done anything at that point to match your regex. Even if $2 was a valid PHP variable name, it still wouldn't be set at that point in your script. Because of this, you can determine that you are using preg_replace improperly and that it may not suit your actual needs.

Note that the preg_replace documentation doesn't support using $n as a separate variable outside of the replacement operation. In other words, 'foo' . $1 . 'bar' is not a valid replacement string, but 'foo$1bar' is.

Depending on the complexity of get_site_url, you have 2 options:

  1. If get_site_url is simply adding a root directory or server name, you could change your replacement string to src="/myotherlocation$2". This will effectively replace "/image/..." with "/myotherlocation/image/..." in the img src. This will not work if get_site_url is doing something more complex.

  2. If get_site_url is complex, you should use preg_replace_callback per other answers. Give the documentation a read and post a new question (or I guess update this question?) if you have trouble with the implementation.

3
votes

PHP variable names cant begin with a number.

3
votes

$2 is not a valid PHP variable. If you meant the second group in the regex then you want to put \2 in a string. However, since you're passing it to a function then you'll need to use preg_replace_callback() instead and substitute appropriately in the callback.

0
votes

if PHP variable begins with number use following:

when I was getting the following as the result set from thrid party API

enter image description here

Code Works

$stockInfo->original->data[0]->close_yesterday

Code Failed

$stockInfo->original->data[0]->52_week_low

Solution

$stockInfo->original->data[0]->{'52_week_high'}