4

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
  • 1
    $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
    Commented Mar 5, 2012 at 18:06
  • Are you trying to pass the second capture group $2 into that function? (Interesting how many people seem to have missed this.)
    – BoltClock
    Commented Mar 5, 2012 at 18:07
  • @BoltClock Yes thats right. I'm trying to pass the second capture group into that function. What should i put there instead of $2 ? Thanks
    – Giri
    Commented Mar 5, 2012 at 18:10
  • 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
    Commented Mar 5, 2012 at 18:25
  • @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. Commented Mar 5, 2012 at 18:55

5 Answers 5

9

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.

4

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"
3
  • 1
    So what does T_LNUMBER mean exactly?
    – Pacerier
    Commented Jun 29, 2015 at 8:27
  • @Pacerier T_LNUMBER means an integer (see here for a list). The parser is expecting to find a valid variable name or another $ but finds the integer 2. The reason is that $2 is an illegal variable name, so the parser will show this message which basically means "unexpected integer found". Commented Jun 29, 2015 at 9:50
  • Everything starts with T_, does "T" mean "token"?
    – Pacerier
    Commented Jul 2, 2015 at 10:37
3

PHP variable names cant begin with a number.

3

$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.

2
  • I'm trying to pass the second capture group into that function. How to put that \2. If you don't mind give me some sample code or can you give me the modified code of my function? Thanks
    – Giri
    Commented Mar 5, 2012 at 18:14
  • Hey there, Sorry.. You've mistaken me. I'm a php beginner. I checked the link you provided. But it looks more advanced. Thats why i'm still asking your help.
    – Giri
    Commented Mar 5, 2012 at 18:19
0

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'}

Not the answer you're looking for? Browse other questions tagged or ask your own question.