note 43611 deleted from function.str-replace by aidan

From: Date: Mon, 28 Jun 2004 10:43:24 +0000
Subject: note 43611 deleted from function.str-replace by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71994@lists.php.net to get a copy of this message
Note Submitter: Jeroen - http://jeroen.all-stars.nl ---- TESTPROGRAM for function str_replace_clean - see below. This program compares functions str_replace() and str_replace_clean(). Put the testprogram as .php file together with the files 'CReplace.php' (see below on this page) and 'test_str_replace_clean_include.php' (see below in this contribution) in the same directory. Then open the testprogram in a browser to see the test-results. <?php require_once('./CReplace.php'); $oReplace = new CReplace; echo '<hr>Test 1 - str_replace_clean(string, array, array)'; $search = "one"; $replace = array("two", "three"); $subject = array("one", "two", "three"); $result_wanted = array("two", "two", "three"); include "./test_str_replace_clean_include.php"; echo '<hr>Test 2 - str_replace_clean(array, array, string) - see the user-contributed note from jr on 03-Jun-2004 at www.php.net/str_replace'; $search = array ('BC', 'ABC'); $replace = array ('E', 'F'); $subject = 'ABC'; $result_wanted = 'AE'; include "./test_str_replace_clean_include.php"; echo '<hr>Test 3 - str_replace_clean(array, array, array)'; $search = array("one", "two", "three"); $replace = array("two", "three", "one"); $subject = array("one", "two", "three"); $result_wanted = array("two", "three", "one"); include "./test_str_replace_clean_include.php"; echo '<hr><p><b>Tests for case-sensitivity</b></p>'; echo '<hr>Test 4a - replace case-sensitive - str_replace_clean(string, string, string, true)'; $search = "H"; $replace = "X"; $subject = "HTML html"; $result_wanted = "XTML html"; $cs="true"; include "./test_str_replace_clean_include.php"; echo '<hr>Test 4b - replace case-INsensitive - str_replace_clean(string, string, string, false)'; $search = "H"; $replace = "X"; $subject = "HTML html"; $result_wanted = "XTML Xtml"; $cs="false"; include "./test_str_replace_clean_include.php"; echo '<hr>Test 5a - replace case-sensitive - str_replace_clean(array, array, array, true)'; $search = array("onE ", "T wo", "ThrEe ", "four"); $replace = array("2 a", "3b c", "-8", "9-"); $subject = array("valueTHREE here", "valuefourhere", "valueone here", "value t wo here"); $result_wanted = array("valueTHREE here", "value9-here", "valueone here", "value t wo here"); $cs="true"; include "./test_str_replace_clean_include.php"; echo '<hr>Test 5b - replace case-INsensitive - str_replace_clean(array, array, array, false)'; $search = array("onE ", "T wo", "ThrEe ", "four"); $replace = array("2 a", "3b c", "-8", "9-"); $subject = array("valueTHREE here", "valuefourhere", "valueone here", "value t wo here"); $result_wanted = array("value-8here", "value9-here", "value2 ahere", "value 3b c here"); $cs="false"; include "./test_str_replace_clean_include.php"; ?> INCLUDE test_str_replace_clean_include.php <?php echo '<br>&#036;search = "'; print_r($search); echo '"'; echo '<br>&#036;replace = "'; print_r($replace); echo '"'; echo '<br>&#036;subject = "'; print_r($subject); echo '"'; echo '<br>The desired result is "'; print_r($result_wanted); echo '"'; echo '<br>Result with standard str_replace(): "'; $result1=str_replace($search, $replace, $subject); print_r($result1); echo '"'; if ($result1==$result_wanted) {echo ' <font color="blue">Okay</font>';} else {echo ' <font color="red">Not the desired result</font>';} echo '<br>Result with str_replace_clean(): "'; if ($cs=="true") { $result2=$oReplace->str_replace_clean($search, $replace, $subject, true); } elseif ($cs=="false") { $result2=$oReplace->str_replace_clean($search, $replace, $subject, false); } else { $result2=$oReplace->str_replace_clean($search, $replace, $subject); } print_r($result2); echo '"'; if ($result2==$result_wanted) {echo ' <font color="blue">Okay</font>';} else {echo ' <font color="red">Not the desired result</font>';} ?>

« previous php.notes (#71994) next »