<?php/*Function: mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )*/ //1==>// 輸出: <body text='black'>/* 這應該是最常見的用法了,從"<body text='%body%'>"中找到"%body%",然後替換成"black"*/$bodytag = str_replace("%body%", "black", "<body text='%body%'>"); //2==>// 輸出: Hll Wrld f PHP/* 參數 search 為數組的用法,逐個地遍曆數組來進行替換*/$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");$onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); //3==>// 輸出: You should eat pizza, beer, and ice cream every day/* 參數 search 和 replace 均為數組的用法,且數組元素的個數相同,進行相互對應的替換*/$phrase = "You should eat fruits, vegetables, and fiber every day.";$healthy = array("fruits", "vegetables", "fiber");$yummy = array("pizza", "beer", "ice cream");$newphrase = str_replace($healthy, $yummy, $phrase); //4==>// Use of the count parameter is available as of PHP 5.0.0/* 輸出匹配次數*/$str = str_replace("ll", "", "good golly miss molly!", $count);echo $count; // 2 //5==>// Order of replacement/* 自訂替換順序,防止重複替換*/$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";$order = array("\r\n", "\n", "\r");$replace = '<br />';// Processes \r\n's first so they aren't converted twice.$newstr = str_replace($order, $replace, $str); //6==>/* 這個是需要注意的,參數 search 和 replace 均為數組,第一次先替換 'a' ,結果為 'apple p',第二次接著替換 'p',因此會出現結果 'apearpearle pear'*/// Outputs: apearpearle pear$letters = array('a', 'p');$fruit = array('apple', 'pear');$text = 'a p';$output = str_replace($letters, $fruit, $text);echo $output;?>