当前位置:在线查询网 > 在线百科全书查询 > ereg_replace

ereg_replace_在线百科全书查询


请输入要查询的词条内容:

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);

?>

相关分词: ereg_replace ereg replace