note 77512 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 23 Mar 2008 23:06:07 +0000 |
| Subject: | note 77512 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137424@lists.php.net to get a copy of this message | ||
Note Submitter: gomodo at free dot fr
----
Some controls problems when you want to replace backslashes '\' ?
Look this example and the protected functions (we try to replace '\' by '.') :
<?php
//****** test with bad double-quote parameter
test_replace_backslash("abcde\2fghij",".");
//******
echo "\n";
//****** test with good simple-quote parameter
test_replace_backslash('abcde\2fghij',".");
//**************************************
//************ functions ****************
function test_replace_backslash($str_test,$str_replace)
{
$str_new= replace_backslash($str_test,".");
if (!$str_new) echo "no readable char found - control call with 'string' and not
".'"'."string".'"';
else echo $str_new;
}
//*****
function replace_backslash($str_to_detect,$str_replace)
{
if(chrs_is_all_readable($str_to_detect)) {
return(preg_replace('[\\\]',$str_replace,$str_to_detect));
}
return (false);
}
//***** inspired from http://fr.php.net/manual/fr/function.chr.php#70931
//***** ( thank to "jgraef at users dot sf dot net")
function chrs_is_all_readable($string)
{
for ($i=0;$i<strlen($string);$i++)
{
$chr = $string{$i};
$ord = ord($chr);
if ($ord<32 or $ord>126) return(false);
}
return (true);
}
?>
this example return :
no readable char found - control call with 'string' and not "string"
abcde.2fghij