note 21382 modified in function.preg-replace by didou
| From: | didou@php.net | Date: | Sat, 17 May 2003 16:32:08 +0000 |
| Subject: | note 21382 modified in function.preg-replace by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-48174@lists.php.net to get a copy of this message | ||
Printing arrays has been tried a few times above but I haven't been able to use the solutions
provided.
Here is what worked for me:
------
$description["useless"] = "moocow";
$description["boring"] = "fubar";
$description["hard"] = "easy now!";
$text = "Arrays in preg are [desc:hard] ";
print preg_replace("/\[desc:([
\w]+)\]/e","\$description['\\1']",$text);
------
This will print "Arrays in preg are easy now! "
The preg finds [desc:hard] and uses "hard" as the index into the array called
$description. Note the /e in the pattern, the function is enclosed in double quotes, and
$description is escaped with \.
--was--
Printing arrays has been tried a few times above but I haven't been able to use the solutions
provided.
Here is what worked for me:
------
$description["useless"] = "moocow";
$description["boring"] = "fubar";
$description["hard"] = "easy now!";
function display_array( $array, $index ) {
return $array[$index];
}
$text = "Arrays in preg are [desc:hard] ";
print preg_replace("/\[desc:([ \w]+)\]/e",
"display_array(\$description,'\\1')",
$text);
------
This will print "Arrays in preg are easy now! "
The preg finds [desc:hard] and uses "hard" as the index into the array called
$description. Note the /e in the pattern, the function is enclosed in double quotes, and
$description is escaped with \.
http://www.php.net/manual/en/function.preg-replace.php