note 24281 modified in function.str-replace by aidan

From: Date: Sun, 27 Jun 2004 13:50:49 +0000
Subject: note 24281 modified in function.str-replace by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71899@lists.php.net to get a copy of this message
The problem is that str_replace seems to call the replace even though there may not be a replacement. This is a problem in that in my parsing script when it finds a certain tag it calls a function, this function does a few SQL queries. It has been noticed that doing a str_replace on a page that contains no 'tag' that I am searching for, the function is called anyway!!! To get around this I have to do a str_pos to see if it exists then call str_replace. eg, <?php // Breadcrumb trail (same as per sitemap) $bc_pos = strpos($string,"[[breadcrumbtrail]]"); if ($bc_pos || is_numeric($bc_pos)) { // true ? then exectue! $string = str_replace ("[[breadcrumbtrail]]", generate_breadcrumb($nodedata_arr, $db), $string); } ?> --was-- The problem is that str_replace seems to call the replace even though there may not be a replacement. This is a problem in that in my parsing script when it finds a certain tag it calls a function, this function does a few SQL queries. It has been noticed that doing a str_replace on a page that contains no 'tag' that I am searching for, the function is called anyway!!! To get around this I have to do a str_pos to see if it exists then call str_replace. eg, // Breadcrumb trail (same as per sitemap) $bc_pos = strpos($string,"[[breadcrumbtrail]]"); if ($bc_pos || is_numeric($bc_pos)) { // true ? then exectue! $string = str_replace ("[[breadcrumbtrail]]", generate_breadcrumb($nodedata_arr, $db), $string); } Grant http://php.net/manual/en/function.str-replace.php

« previous php.notes (#71899) next »