note 78483 deleted from function.strpos by levim

From: Date: Tue, 22 Jul 2014 03:19:54 +0000
Subject: note 78483 deleted from function.strpos by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-200008@lists.php.net to get a copy of this message
Note Submitter: Benjie ---- str_replace evaluates its arguments exactly once. for example: <?php $page = str_replace("##randompicture##", getrandompicture(), $page); ?> will call getrandompicture() once, ie it will insert the same random picture for each occurrence of ##randompicture## :( Here is my quick and dirty workaround: <?php function add_random_pictures($text) { while (($i = strpos($text, "##randompicture##")) !== false) { $text = substr_replace($text, getrandompicture(), $i, strlen("##randompicture##")); } return $text; } $page = add_random_pictures($page); ?>

« previous php.notes (#200008) next »