php之str

str_replace

(PHP 4, PHP 5)

str_replace — Replace all occurrences of the search string with the replacement string

Description

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ]
)

This function returns a string or an array with all occurrences of search in subject replaced
with the givenreplace value.

If you don't need fancy replacing rules (like regular expressions), you should always use this function instead ofpreg_replace().

Parameters

If search and replace are
arrays, then str_replace() takes a value from each array and uses them to search and replace on subject.
If replace has fewer values than search,
then an empty string is used for the rest of replacement values. If search is
an array and replace is a string, then this replacement string is
used for every value of search. The converse would not make sense,
though.

If search or replace are
arrays, their elements are processed first to last.

search

The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.

replace

The replacement value that replaces found search values.
An array may be used to designate multiple replacements.

subject

The string or array being searched and replaced on, otherwise known as the haystack.

If subject is
an array, then the search and replace is performed with every entry of subject,
and the return value is an array as well.

count

If passed, this will be set to the number of replacements performed.

Return Values

This function returns a string or an array with the replaced values.

Changelog

Version Description
5.0.0 The count parameter
was added.
4.3.3 The behaviour of this function changed. In older versions a bug existed when using arrays as bothsearch and replace parameters
which caused empty search indexes to be skipped without advancing the
internal pointer on the replace array. This has been corrected in PHP
4.3.3, any scripts which relied on this bug should remove empty search values prior to calling this function in order to mimic the original behavior.
4.0.5 Most parameters can now be an array.

Examples

Example #1 Basic str_replace() examples

<?php
// Provides:
$bodytag = str_replace("%body%", "black", "");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: 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);

// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>

Example #2 Examples of potential str_replace() gotchas

<?php
// Order of replacement
$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order = array("\r\n", "\n", "\r");
$replace = '
';

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);

// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);

// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
?>

Notes

Note: This function is binary-safe.

时间: 2024-10-29 21:08:44

php之str的相关文章

char *str 与char str[] 的区别

简述 char *str = "abcd" 先在静态区为"hello"常量分配5Byte,接着在栈里为指针str分配4Byte(32位机器)并指向"abcd"字串的首地址,因此此时str是指向第一个字符'a'的一个指针. char str[ ] = "abcd" 在栈里分配连续的5Byte,内容为'a','b','c','d','\0',并把首地址标记为str,此时str是数组名,同时也是指向数组第一个字符'a'的指针常量.

数据库主键int型和str型效率比较

比较|数据|数据库 又看到这样的一段话: 主键用整型会极大的提高查询效率,而字符型的比较开销要比整型的比较开销大很多,用字符型数据作主键会使数据插入.更新与查询的效率降低.数据量小的时候这点降低可能不会被注意,可是当数据量大的时候,小的改进也能够提高系统的响应速度. 我做了一个实验,用MSSQL企业管理器建立两张表TInt和TStr, TInt { intId int PRIMARY KEY intValue int } TStr { strId varchar(20) PRIMARY KEY

php函数之子字符串替换&amp;amp;#65279; str

str_replace - 子字符串替换 [str_replace]mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )php函数str_replace: 返回一个字符串或者数组.该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果. 现在我们所能知道的一些这个函数的用法,如:str_replace("#", &q

isNull(str), isEmpty(str)和 str = 的区别

区别 1.isnull()Null 值指出变量不包含有效数据.Null 与 Empty 不同,后者指出变量未经初始化.Null 与零长度字符串 ("") 也不同,零长度字符串往往指的是空串. 重点 使用 IsNull 函数可以判断表达式是否包含 Null 值.在某些情况下想使表达式取值为 True,例如 IfVar=Null 和 IfVar<>Null,但它们通常总是为 False.这是因为任何包含 Null 的表达式本身就为 Null,所以表达式的结果为 False. 下

stringstream.str()字符串用法的陷阱

在编写应用程序时,我们经常要使用到字符串.C++标准库中的<string>和<sstream>为我们 操作字符串提供了很多的方便,例如:对象封装.安全和自动的类型转换.直接拼接.不必担心越界等等.但 今天我们并不想长篇累牍得去介绍这几个标准库提供的功能,而是分享一下stringstream.str()的一个有趣的 现象.我们先来看一个例子: 1 #include <string> 2 #include <sstream> 3 #include <ios

比较strtr, str

之前已经分析过strtr的源码了,现在就比较strtr, str_replace和preg_replace的效率: 复制代码 代码如下: $str = '111111110000000000000000000000000000000111000001000100010000010010000010010000010100000010 '; $str = str_repeat($str, 1); $pattern1 = array('12345'=>'', '67891'=>''); $patt

光驱导致的STR功能失常

   眼下不少主板都同时支持ATA100和STR.但ATA100和STR这两种新技术还有些"娇嫩",拥有支持这两种新技术的主板和硬盘,并不一定就可以让我们轻松享受由它们带来的好处,如果安装调试不得法,在某些系统中,某些硬件可能会跟你捣乱.笔者最近就遇到了这种麻烦:爱机进入"等待"状态后,我的北极熊光驱总是沉睡不醒,造成STR功能失常,要热启动后才能唤醒电脑. 笔者机子的基本配置为PⅢ866CPU.技嘉GA-60XM7E主板(i815E).IBM腾龙Ⅱ代30GB硬盘(

[Python爬虫] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题

        最近研究搜索引擎.知识图谱和Python爬虫比较多,中文乱码问题再次浮现于眼前.虽然市面上讲述中文编码问题的文章数不胜数,同时以前我也讲述过PHP处理数据库服务器中文乱码问题,但是此处还是准备简单做下笔记.方便以后查阅和大家学习.        中文编码问题的处理核心都是--保证所有的编码方式一致即可,包括编译器.数据库.浏览器编码方式等,而Python通常的处理流程是将unicode作为中间转换码进行过渡.先将待处理字符串用unicode函数以正确的编码转换为Unicode码,

Python字符串(Str)详解

字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可 字符串的格式 b = "hello itcast.cn" # 或者 b = 'hello itcast.cn' 双引号或者单引号中的数据,就是字符串 字符串连接的方法 直接通过加号(+)操作符连接 a = "str1" b = "str2" c = a + b print("a:%s" %