Welcome Guest, Not a member yet? Register   Sign In
preg replace (again -.-)
#1

[eluser]Young Caveman[/eluser]
Hi!
PHP function preg_replace seems to be something which I'll never completely understand, I suppose Smile
Here's the code:
Code:
$layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace('$1').'</div>', $layout);
I have to replace everything which looks like this:
Code:
{some}
to this:
Code:
<div class="some"> stuff </div>
The problem appears because inside the div tag I have to print something which depends from what I have inside the brackets, but I can't pass the variable '$1' in my method $this->replace() becauase PHP sees it as just "$1".
Where's my mistake? Can't found it :-(
Thank you, in advance, guys. Smile
#2

[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.
#3

[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 Smile
Here's the code:
Code:
$layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace('$1').'</div>', $layout);
I have to replace everything which looks like this:
Code:
{some}
to this:
Code:
<div class="some"> stuff </div>
The problem appears because inside the div tag I have to print something which depends from what I have inside the brackets, but I can't pass the variable '$1' in my method $this->replace() becauase PHP sees it as just "$1".
Where's my mistake? Can't found it :-(
Thank you, in advance, guys. Smile[/quote]

remove the '' Wink from '$1' in replace('$1') ...
Code:
$layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace($1).'</div>', $layout);
#4

[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:
#5

[eluser]Young Caveman[/eluser]
[quote author="amrtami" date="1218223564"]
remove the '' Wink from '$1' in replace('$1') ...
Code:
$layout = preg_replace('~{(.+?)}~', '<div class="$1">'.$this->replace($1).'</div>', $layout);
[/quote]

Code:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'

:down:
#6

[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.
#7

[eluser]Young Caveman[/eluser]
Ok thank you guys.
#8

[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);

function replace_function($matches)
{
    $match = $matches[1]; // 0 is the whole thing, 1 is the first subpattern
    
    // Do something with $match

    return '<div class="'.$matches[1].'">'.$match.'</div>';
}
#9

[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);

function replace_function($matches)
{
    $match = $matches[1]; // 0 is the whole thing, 1 is the first subpattern
    
    // Do something with $match

    return '<div class="'.$matches[1].'">'.$match.'</div>';
}
[/quote]

Oh, Wow. :|
This works great. Smile Really, thank you inparo. Smile
#10

[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);

if(count($match) > 0)
{
   $layout = str_replace('{'.$match[0][0].'}','<div class="'.$match[0][0].'">'.$this->replace($match[0][0]).'</div>',$layout);
}
If you want to catch multiple matches you have to use the preg_match_all function and do a loop to replace the patterns.




Theme © iAndrew 2016 - Forum software by © MyBB