帝国CMS模板大全
www.admin99.cn
www.92cms.cn 帝国CMS模板下载站!,情怀,养站,二次开发!源码需求比较大的一站式会员下载,价更省!!!

php ereg_replace函数 - php函数

php ereg_replace函数

$string  =  "this is a test" ; 
echo   str_replace ( " is" ,  " was" ,  $string ); 
echo   ereg_replace ( "( )is" ,  "1was" ,  $string );  //其中1就是第一个括号中空格  
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 提出的例子,代码如下:

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

echo   "$textn" ; 
// this is a fine day, not overcast and rainy.   
?> 

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

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

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《php ereg_replace函数 - php函数》
文章链接:https://www.admin99.cn/7066
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
QQ站长交流群:953950264

登录

找回密码

注册