regex backreferences in PHP (preg_replace)
Posted: Fri Mar 21, 2014 6:46 pm
A little backstory: I am writing a plugin for a bb forum (mybb to be exact) which parses the posted message and replaces some BB code
sample synthax is:
The regex code itself works (adapted it from a sample plugin), but I want to have the returned regex string
in a variable, which I can check if it was set.
Here's the code:
Example:
Just one match, so $second is not returned, and $first is a string containing "com.google.android.keep"
Now I check if $second is actually "existing"
it doesn't work
so I did a var_dump. It tells me it always contains the string "$2"
So I cannot simply check:
because it will always pass.
Now if I print out the variable $second I get an empty string returned (because it gets replaced by the regex)
Same as above
Practically the same...
If I print out the variable $second I get "com.google.android.keep" returned (because it also gets replaced by the regex)
How do I solve this tricky situation?
I cannot simply do a search and replace, because I need to load the theme (horizontal in the above example),
than vary the output, like placing a different div element which is bigger/smaller/wider/thinner/etc
and pass the other variable to a different script to load some external data
I know, it's a pretty wall of text, but I tried to be as exact as possible
sample synthax is:
Code: Select all
[gplay=first_argument]second_argument[/gplay]
in a variable, which I can check if it was set.
Here's the code:
Code: Select all
<?php function bbcode_input($first, $second)
{
$return = "Length: " . strlen(strval($second)) . " - " . $second; // length is always 2, but does not output anything if printed
$return .= var_dump($second); //always tells me it's 2 chars long and contains "$2"
return $return;
}
function gplay_infobox_run($message)
{
$pattern = array("#\[gplay=(?:"|\"|')?(.*?)[\"']?(?:"|\"|')?\](.*?)\[\/gplay\](\r\n?|\n?)#si", "#\[gplay\](.*?)\[\/gplay\](\r\n?|\n?)#si");
$replace = bbcode_input("$1", "$2"); // there it is <----
while(preg_match_all($pattern[0], $message, $hello) or preg_match_all($pattern[1], $message, $hello))
{
$message = preg_replace($pattern, $replace, $message);
}
return $message;
}
?>Code: Select all
[gplay]com.google.android.keep[/gplay]Now I check if $second is actually "existing"
Code: Select all
<?php
if($second)
...or...
if(empty($second))
...or...
if(isset($second))
?>so I did a var_dump. It tells me it always contains the string "$2"
So I cannot simply check:
Code: Select all
<?php
if($second=="$2")
...
?>Now if I print out the variable $second I get an empty string returned (because it gets replaced by the regex)
Code: Select all
[gplay=com.google.android.keep][/gplay]Code: Select all
[gplay=horizontal]com.google.android.keep[/gplay]If I print out the variable $second I get "com.google.android.keep" returned (because it also gets replaced by the regex)
How do I solve this tricky situation?
I cannot simply do a search and replace, because I need to load the theme (horizontal in the above example),
than vary the output, like placing a different div element which is bigger/smaller/wider/thinner/etc
and pass the other variable to a different script to load some external data
I know, it's a pretty wall of text, but I tried to be as exact as possible