字串的取代方式有好幾種

1.ereg_replace(string pattern, string replacement, string string)

剖析字串並取代

ex:




$string = "This is a test";



echo ereg_replace (" is", " was", $string); //結果This was a test



echo ereg_replace ("( )is", "\\1was", $string); //結果This was a test

\\1代表第一個括號



echo ereg_replace ("(( )is)", "\\2was", $string); //結果This was a test

\\2代表第二個括號

?>

http://www.php5.idv.tw/modules.php?mod=books&act=show&shid=553



2.str_replace --- 取代所有在字串中出現的字串



語法 : string str_replace (string needle, string str, string haystack)

ex:

$string = "insert into `matcust` VALUES (\\\"\\\"2334', '234', NOW( ) , '342'); ";

$string=str_replace ("\\\"", " fdff", $string)."
";

\\結果insert into `matcust` VALUES ( fdff fdff2334', '234', NOW( ) , '342');

但這個結果並不能夠滿足我

因為當我在textarea輸入有\"的符號時,並用addslashes處理後

結果\"的符號並沒有被取代

上述的兩個方式都失敗了,可能是\與"連在一起時又產生某些內碼了吧

所以為了能夠在textarea輸入sql語法,並且避免掉許功蓋的big5問題



我將程式寫成下面這樣

function metaslash($zz) //函數規格化' " \對於以上符號加入\倒斜線

{

if (!get_magic_quotes_gpc()) {

srand((double)microtime()*1000000); //亂數

$tempname = " ^^^ ".rand(); //亂數名稱

$zz2=str_replace ("\"", $tempname , $zz); //使用$temp來取代",再將$temp換回"

$zz2=htmlspecialchars(addslashes($zz2));

$zz2=str_replace ("\\'", "'", $zz2);

$zz2=str_replace ($tempname, "\"" , $zz2);

return $zz2;

}

else {

return htmlspecialchars($zz);

}

}//function zdate($zz)

這樣就可將避免許功蓋,又可讓addslashes不亂加入'"這兩個符號

其實"算是有點取巧

我先用一個變數去取代"

再用"取代回來

沒辦法呀,我真的想不出來了
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 killworm737 的頭像
    killworm737

    紀錄些小事情

    killworm737 發表在 痞客邦 留言(0) 人氣()