词条 | ereg_replace |
释义 | 函数描述:string Ereg_replace(string pattern,string replacement,string string) 说明:函数Ereg_replace可用于替换文本,当参数pattern与参数string中的字串匹配时,他就被参数replacement的内容所替换。若参数pattern中包含有圆括号的子表达式,则在参数replacement中可以用包含特定的代码来说明哪个子表达式被替换,最多可以有九个子表达式。其具体形式是用两个反斜杠后跟一个从0~9的单数字,0表示与整个表达式相匹配,1~9表示相应的与前1~9个子表达式相匹配。注意,参数pattern中的圆括号是可以嵌套的,其表达式序号等于该表达式前的圆括号的数目。 返回值:函数ereg_eplace返回替换后的字符串pattern。 1. ereg_replace() 例子<?php $string = "This is a test"; echo str_replace(" is", " was", $string); echo ereg_replace("( )is", "\\\\1was", $string); echo ereg_replace("(( )is)", "\\\\2was", $string); ?> 要注意的一点是如果在 replacement 参数中使用了整数值,则可能得不到所期望的结果。这是因为 ereg_replace() 将把数字作为字符的序列值来解释并应用之。 2. ereg_replace() 例子<?php /* 不能产生出期望的结果 */ $num = 4; $string = "This string has four words."; $string = ereg_replace('four', $num, $string); echo $string; /* Output: 'This string has words.' */ /* 本例工作正常 */ $num = '4'; $string = "This string has four words."; $string = ereg_replace('four', $num, $string); echo $string; /* Output: 'This string has 4 words.' */ ?> 3. 将 URL 替换为超连接<?php $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\\"\\\\0\\">\\\\0</a>", $text); ?> |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。