note 77512 added to function.str-replace
| From: | gomodo at free dot fr | Date: | Sat, 01 Sep 2007 20:30:40 +0000 |
| Subject: | note 77512 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130073@lists.php.net to get a copy of this message | ||
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
----
Server IP: 217.174.196.197
Probable Submitter: 82.242.125.194
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/77512
Del: integrated -- https://master.php.net/note/delete/77512/integrated
Del: useless -- https://master.php.net/note/delete/77512/useless
Del: bad code -- https://master.php.net/note/delete/77512/bad+code
Del: spam -- https://master.php.net/note/delete/77512/spam
Del: non-english -- https://master.php.net/note/delete/77512/non-english
Del: in docs -- https://master.php.net/note/delete/77512/in+docs
Del: other reasons-- https://master.php.net/note/delete/77512
Reject -- https://master.php.net/note/reject/77512
Search -- https://master.php.net/manage/user-notes.php