preg replace (again -.-) |
[eluser]Young Caveman[/eluser]
Hi! PHP function preg_replace seems to be something which I'll never completely understand, I suppose ![]() Here's the code: Code: $layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace('$1').'</div>', $layout); Code: {some} Code: <div class="some"> stuff </div> Where's my mistake? Can't found it :-( Thank you, in advance, guys. ![]()
[eluser]xwero[/eluser]
There is a difference between a surrounding a string with a single or a double quote. With double quotes php checks if there are variables and with single quotes it doesn't.
[eluser]amrnt[/eluser]
[quote author="Don Robin." date="1218223256"]Hi! PHP function preg_replace seems to be something which I'll never completely understand, I suppose ![]() Here's the code: Code: $layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace('$1').'</div>', $layout); Code: {some} Code: <div class="some"> stuff </div> Where's my mistake? Can't found it :-( Thank you, in advance, guys. ![]() remove the '' ![]() Code: $layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace($1).'</div>', $layout);
[eluser]Young Caveman[/eluser]
[quote author="xwero" date="1218223462"]There is a difference between a surrounding a string with a single or a double quote. With double quotes php checks if there are variables and with single quotes it doesn't.[/quote] Code: $layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace("$1").'</div>', $layout); Like this? If you meant that, it did not work. :down:
[eluser]Young Caveman[/eluser]
[quote author="amrtami" date="1218223564"] remove the '' ![]() Code: $layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace($1).'</div>', $layout); Code: Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' :down:
[eluser]xwero[/eluser]
Ok my mistake. You can't pass the found pattern to another function/method because it's kept internally. if you want to do something you want you need to find another solution.
[eluser]Pascal Kriete[/eluser]
Use preg_replace_callback if you need to process one of the captured strings. Code: $layout = preg_replace_callback('~{(.+?)}~', array($this, 'replace_function'), $layout);
[eluser]Young Caveman[/eluser]
[quote author="inparo" date="1218225361"]Use preg_replace_callback if you need to process one of the captured strings. Code: $layout = preg_replace_callback('~{(.+?)}~', array($this, 'replace_function'), $layout); Oh, Wow. :| This works great. ![]() ![]()
[eluser]xwero[/eluser]
This is a way to do it without adding another function to your code. Code: preg_match('~{(.+?)}~', $layout, $match, PREG_OFFSET_CAPTURE); |
Welcome Guest, Not a member yet? Register Sign In |