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");$yummy = array("pizza", "beer", "ice cream");$newphrase = str_replace($healthy, $yummy, $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如何去掉指定字符?的详细内容。