PHP若何去掉指定字符?
正在PHP中能够应用“str_replace()”函数去掉指定字符,该函数会将子字符串进行交换,其语法是“str_replace(sea,rep,sub) ”,应用时只要挪用该函数将指定字符交换为空便可。
代码示例
根本典范榜样
<?php // 赋值: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); // 赋值: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); // 赋值: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yu妹妹y = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yu妹妹y, $phrase); // 赋值: 2 $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; ?>
交换典范榜样
<?php // 交换程序 $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // 起首交换 \r\n 字符,因而它们没有会被两次转换 $newstr = str_replace($order, $replace, $str); // 输入 F ,由于 A 被 B 交换,B 又被 C 交换,以此类推... // 因为从左到右顺次交换,终极 E 被 F 交换 $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject); // 输入: apearpearle pear // 因为下面提到的缘由 $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text); echo $output; ?>
保举教程:《PHP》
以上就是PHP若何去掉指定字符?的具体内容,更多请存眷资源魔其它相干文章!
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!