PHP str_replace() 函数的常见用法(转载)
2013-02-22 21:41
351 查看
原文地址:http://www.cnblogs.com/mo-beifeng/archive/2011/05/12/2044376.html
<?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;
?>
<?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;
?>
相关文章推荐
- PHP str_replace() 函数的常见用法
- PHP str_replace() 函数 & strtr() 函数 的常见用法
- 常见的四种排名函数的用法----转载
- PHP str_replace() 函数 & strtr() 函数 的常见用法
- PHP str_replace() 函数的常见用法
- PHP字符串替换str_replace()函数4种用法详解
- 【转载】消息模式Toast.makeText的几种常见用法
- PHP str_replace() 函数
- (转载)OpenCV 函数的参数用法…
- 关于OpenCV中常见函数用法总结
- C++ 中string.find() 函数的用法总结(转载)
- MSP430的看门狗常见用法以及中断函数的书写方法
- c++: string 的函数replace()用法
- 【转载】Js中replace()的用法
- 【转载】assert() 函数用法
- C++ 中string.find() 函数的用法总结(转载)
- DOM节点的替换或修改函数replaceChild()用法实例
- JavaScript中常见的字符串操作函数及用法
- orcle函数常见用法
- CListBox类常见函数用法