note 21382 added to function.preg-replace

From: Date: Fri, 10 May 2002 18:36:29 +0000
Subject: note 21382 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-30432@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!"; 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 http://master.php.net/manage/user-notes.php?action=edit+21382 http://master.php.net/manage/user-notes.php?action=delete+21382 http://master.php.net/manage/user-notes.php?action=reject+21382

« previous php.notes (#30432) next »