Page 1 of 1

[solv] preg_replace problem (trying to bring out a variable)

Posted: Sat Feb 24, 2007 10:27 am
by xcow
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

$variable["new"]="hi";
$variable["cow"]="wow";
$string="nice to meet you [phpfile=new]";
$pattern='/\[phpfile=(\w+)\]/i';
$replace=$variable["$1"];
preg_replace($pattern,$replace,$string);
As you can see here I am trying to replace:

[phpfile=new]

with:

hi (from variable $variable["new"])

but the server says $1 is not registered.

any help is appreciated, thx.. =p


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Feb 24, 2007 11:04 am
by feyd
I would normally prefer to use preg_replace_callback() for this, but the following should work, but is untested.

Code: Select all

$variable["new"]="hi";
$variable["cow"]="wow";
$string="nice to meet you [phpfile=new]";

$g = create_function('$a', 'return preg_quote($a, \'~\');');
$k = array_keys($variable);
$k = array_map($g, $k);
$pattern = '~\\[phpfile=(?i:'.'(' . implode('|',$k) . '))\\]~e';
$replace = '$variable[strtolower("$1")]';
$newString = preg_replace($pattern, $replace, $string);

Posted: Sat Feb 24, 2007 3:17 pm
by xcow
thank you for your help, but the code doesn't work, it gives these errors:


Warning: array_keys(): The first argument should be an array in /home/www/include/parser.php on line 33
Warning: array_map(): Argument #2 should be an array in /home/www/include/parser.php on line 34
Warning: implode(): Bad arguments. in /home/www/include/parser.php on line 35

and i actually have read the preg_replace_callback(), but i didn't understand any of the tutorials and references i searched on google, so i was just wondering how i would do it with the preg_replace_callback() function.

this is where i am up to:

Code: Select all

$mylist=array();
$variable["new"]="hi";
$variable["cow"]="wow";
$string="nice to meet you [phpfile=new]"; 

function my_callback($match) {
   global $mylist;
   $mylist[]=array_merge($match);
   return 1;
}

echo "<p>".preg_replace_callback("/\[phpfile=(\w+)\]/i", "my_callback", $string);
echo "<p>"; print_r($mylist);
but i don't know how to work with arrays to get what i needed: $variable["new"]="hi";

Posted: Sat Feb 24, 2007 4:27 pm
by feyd
The code I posted works. Did you alter something?

Posted: Sat Feb 24, 2007 4:45 pm
by xcow
wow this is weird... sry i just tested the code u posted, it works.. but when i put it ins my script it gives me those errors

main.php:

Code: Select all

include("games.php");
$string = "test [phpfile=readtitle] - title  <B>[phpfile=readsource]</B>]";
$g = create_function('$a', 'return preg_quote($a, \'~\');');
$k = array_keys($phpfile_output);
$k = array_map($g, $k);
$parser_content_pattern[0] = '~\\[phpfile=(?i:'.'(' . implode('|',$k) . '))\\]~e';
$parser_content_replace[0] = '$phpfile_output[strtolower("$1")]';
$pro = preg_replace($parser_content_pattern, $parser_content_replace, $string);
games.php:

Code: Select all

$phpfile_output["readtitle"] = "testtitle";
$phpfile_output["readsource"] = "testcontent";
i also want to mention that $string has gone through fopen and fread and explode to be displayed as a string.

Posted: Sat Feb 24, 2007 5:04 pm
by xcow
ahhhhhhh... sry mistake on my part.. in one of my code i mis-wrote the variable name... thx it works =p