php ereg_replace函数基础与实例代码
发布时间:2022-06-17 09:15:56 所属栏目:PHP教程 来源:互联网
导读:语法:string ereg_replace(string $pattern,string $replacement,string $string) 修改后的字符串返回,如果没有找到匹配的字符串,那么将返回不变,实例代码如下: $string = this phpfensi.com a test; echo str_replace( phpfensi.com, was, $string); echo e
语法:string ereg_replace(string $pattern,string $replacement,string $string) 修改后的字符串返回,如果没有找到匹配的字符串,那么将返回不变,实例代码如下: $string = "this phpfensi.com a test"; echo str_replace(" phpfensi.com", " was", $string); echo ereg_replace("( )phpfensi.com", "1was", $string); echo ereg_replace("(( )phpfensi.com)", "2was", $string); 有一点要注意的是,如果你使用一个整数参数值作为替代,您可能不会得到你期望的结果,这是因为ereg_replace()将解释为一个字符值序数,并套用,例如如下代码: $num = 4; $string = "this string has four words."; $string = ereg_replace('four', $num, $string); echo $string; /* output: 'this string has words.' */ /* this will work. */ $num = '4'; $string = "this string has four words."; $string = ereg_replace('four', $num, $string); echo $string; /* output: 'this string has 4 words.' */ 来看一个用ereg_replace获取连接代码,代码如下: $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=" |