网站地图    收藏   

主页 > php专栏 > php函数大全 >

php ereg_replace函数 - php函数

来源:自学PHP网    时间:2014-11-25 00:26 作者: 阅读:

[导读] $string=thisisatest;echostr_replace(is,was,$string);echoereg_replace(()is,1was,$string); 其中1就是第一个括号中空格echoereg_replace((()is),2was,$string); 其中2就是第二...

php ereg_replace函数

  1. $string = "this is a test"
  2. echo str_replace(" is"" was"$string); 
  3. echo ereg_replace("( )is""1was"$string); //其中1就是第一个括号中空格 
  4. echo ereg_replace("(( )is)""2was"$string); //其中2就是第二个括号的空格,上面三行也就是把" is"替换为" was";都有空格的。 

ereg_replace ( string pattern, string replacement, string string)

也就是说如果pattern中带有()得字符串(如;括号中有空格),那你的replacement就可以使用如1的字符串,那这个1就给可以用你第1个括号中字符串来替换,如果是2那就用第2个括号中的字符串替换.

括号从左到右,以左括号为准,下面那个是去掉url中的如&page=1符串比对解析并取代.

语法: string ereg_replace(string pattern, string replacement, string string);

返回值:字符串

函数种类:资料处理

内容说明:本函数以 pattern 的规则来解析比对字符串 string,欲取而代之的字符串为参数 replacement,返回值为字符串类型,为取代后的字符串结果.

使用范例,ken@freebsdrocks.com 在 16-mar-1999 提出的例子,代码如下:

  1. <?php 
  2. $text = 'this is a {1} day, not {2} and {3}.'
  3. $daytype = array( 1 => 'fine'
  4.                   2 => 'overcast'
  5.                   3 => 'rainy' ); 
  6. while (ereg ('{([0-9]+)}'$text$regs)) { 
  7.   $found = $regs[1]; 
  8.   $text = ereg_replace("{".$found."}"$daytype[$found], $text); 
  9. echo "$textn"
  10. // this is a fine day, not overcast and rainy.  
  11. ?> 

ken@freebsdrocks.com 并同时提出具有相同功能的perl 程序范例如下:

  1. $text = 'this is a {1} day, not {2} and {3}.'
  2. %daytype = ( 1 => 'fine'
  3.              2 => 'overcast'
  4.              3 => 'rainy' ); 
  5. $text =~ s/{(d+)}/$daytype{$1}/eg; 
  6. print "$textn"

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论