Jump to content

Recommended Posts

I have this class

 

class BBParse {

  public static function maxsize($call, $size) {

  echo $size;
if ($call == "size" && $size > "4") {$size = "4";}
return $size;

}
}

 

And this preg_replace

 

			$string = preg_replace('/\['.$tag.'=(.+?)\](.+?)\[\/'.$tag.'\]/is','<'.$data[0].' '.$data[1].'='.BBParse::maxsize($data['1'], '$1').'>$2</'.$data[0].'>',$string);

 

 

This part of the preg_replace is the issue BBParse::maxsize($data['1'], '$1')

 

As you can see I am trying to manipulate the value of $data['1'] and $1. The $data['1'] is seen by maxsize() no problem, but $1 is seen by maxsize() as $1, as in that is the value when in actual fact the value of $1 is always 1,2,3 or 4

 

Its doing that because I have it wrapped in appostrophies '$1' so it sees it as a string, but if I take them off, the script dies

 

Any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/137678-preg_replace-function/
Share on other sites

If you wrap something in single quotes it uses it as a string literal without parsing its content save for single quote escapes.

 

<?php
$test = 'value';
echo 'test: $test'; // "test: $test"
echo "test: $test"; // "test: value"
?>

 

and a variable can only start with a letter or an underscore, so $1 is an invalid variable name.

 

Edit:

And please don't wrap all of your literals, including ints, in quotes. It's inefficient as php will have to cast them back before using them as the types for which you wish to use them... plus it's anoying to read.

 

use $variable, not "$variable". 7, not "7" or '7'. Sorry, pet peeve I've been seeing WAY too much lately.

genericnumber1:

 

In preg_replace() $1, $2, etc. are valid variables that can be used within the replacement parameter..

 

http://uk3.php.net/manual/en/function.preg-replace.php

 

Also:

 

$test = 'value';
echo 'test: $test';

 

...will still produce "test: value" ..

 

 

jaymc:

 

Quite fine to use $1, $2, etc but I'm not sure you can use them to call another function from within preg_replace?? You need to remove the single quotes from the arrays though, unless you have created them that way?

 

$data['1'] = 'foo'; // valid way to do it but..
print $data[1]; // ... won't work!
print $data['1']; // .... will!

$data[2] = 'bar!'; // valid way to do it but..
print $data['2']; // ... won't work!
print $data[2]; // ... will!

 

Remember 1 != '1' ..

 

I'm not saying this will definitely work though..

 

A

$1, etc aren't variables, they're replacement commands... similar to %1, etc. I certainly wouldn't consider them variables.

 

Also:

 

$test = 'value';
echo 'test: $test';

 

...will still produce "test: value" ..

 

and no, it won't. Try it. http://www.google.com/search?q=difference+between+single+and+double+quotes+in+php

Hah boy do I feel foolish! :-[

 

Though I would still consider them variables being as they represent...variable data?

 

Perhaps they could be referred to as variables, but since they accept things like ${1}1 I think of them more as formats than variables... I suppose it's all just semantics.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.