php专区

 首页 > php专区 > PHP应用 > php函数大全 > php substr_replace替换字符串一些实例 - php函数

php substr_replace替换字符串一些实例 - php函数

分享到:
【字体:
导读:
          substr_replace与str_replace有一点像就是直接把字符串替换一部份了,下面小编来给各位同学介绍一下操作方法。substr_replace() 函数把字符...

php substr_replace替换字符串一些实例

substr_replace与str_replace有一点像就是直接把字符串替换一部份了,下面小编来给各位同学介绍一下操作方法。

substr_replace() 函数把字符串的一部分替换为另一个字符串。

用法:

substr_replace(string,replacement,start,length)

注意当字符串包含中文时,不经过特殊处理会出现乱码,代码如下:

  1. $string1="123456789";    
  2.    
  3. echo substr_replace($string1,'###',0);  
  4. //###  
  5. echo substr_replace($string1,'###',5);  
  6. //12345###    
  7.    
  8. echo substr_replace($string1,'###',0,0);  
  9. //###123456789  
  10.    
  11. echo substr_replace($string1,'###',8,-2);  
  12. //12345678###9  
  13.    
  14. echo substr_replace($string1,'###',-6,-1);  
  15. //123###9  
  16. echo "n";  
  17. echo substr_replace($string1,'###',-1);  
  18. //123###9  
  19. echo "n";  
  20. echo substr_replace($string1,'###',1,-1);  
  21. //1###9  
  22.    
  23. echo substr_replace($string1,'###',1,1);  
  24. //1###3456789  
  25. ?> 

例2代码如下:

  1.  $var = 'ABCDEFGH:/MNRPQR/'
  2.  echo "Original: $varn"
  3.  /* These two examples replace all of $var with 'bob'. */ 
  4.  echo substr_replace($var'bob', 0) . "n"
  5.  echo substr_replace($var'bob', 0, strlen($var)) . "n"
  6.  /* Insert 'bob' right at the beginning of $var. */ 
  7.  echo substr_replace($var'bob', 0, 0) . "n"
  8.  /* These next two replace 'MNRPQR' in $var with 'bob'. */ 
  9.  echo substr_replace($var'bob', 10, -1) . "n"
  10.  echo substr_replace($var'bob', -7, -1) . "n"
  11.  /* Delete 'MNRPQR' from $var. */ 
  12.  echo substr_replace($var'', 10, -1) . "n"
  13. ?> 

将过长的字符串用省略号代替一部分,下面的程序可以将过长的字符串保留首尾,中间用省略号代替,代码如下:

  1.  $longString = 'abcdefghijklmnopqrstuvwxyz0123456789z.jpg'
  2.  $separator = '...'
  3.  $separatorlength = strlen($separator) ; 
  4.     // 需要保留的字符串 
  5.  $maxlength = 25 - $separatorlength
  6.     // 从一半的长度开始 
  7.  $start = $maxlength / 2 ; 
  8.     // 计算偏移量 
  9.  $trunc =  strlen($longString) - $maxlength
  10.  echo substr_replace($longString$separator$start$trunc); 
  11.  //prints "abcdefghij...56789z.jpg" 
  12. ?> 
  13. //程序运行结果: 
  14. //abcdefghijk...456789z.jpg 

Program List:将多出的字符用省略号代替,代码如下:

  1. function truncate($text,$numb)  
  2.  $text = html_entity_decode($text, ENT_QUOTES); 
  3.  if (strlen($text) > $numb)  
  4.  { 
  5.   $text = substr($text, 0, $numb); 
  6.   $text = substr($text,0,strrpos($text," ")); 
  7.      //This strips the full stop: 
  8.      if ((substr($text, -1)) == ".")  
  9.   { 
  10.          $text = substr($text,0,(strrpos($text,"."))); 
  11.      } 
  12.   $etc = "...";  
  13.   $text = $text.$etc
  14.  }  
  15.  $text = htmlentities($text, ENT_QUOTES);  
  16.  return $text
  17. //Call function 
  18. $text = 'welcome to nowamagic, welcome to nowamagic, welcome to nowamagic'
  19. $result = truncate($text, 35); 
  20. echo $result
  21. ?> 

好了你大概会知道此函数的作用了.

分享到:
php函数的传值与传址(引用)详解 - php函...
php函数的传值与传址(引用)详解 在php中我们函数传值就比较简单了,但可能有些朋友地天真无邪函数传址或引用搞不明白,下面小编来给各位介绍在php中函数传值与传址(引用)介绍,希望对各位有所帮助。 php中引用的用法: 1. 变量的引用赋值: $a = &$b 2. 函数调用时的引用参数传递    1) 早...
php手机号中间几位替换星号实例 - php函...
php手机号中间几位替换星号实例 正则表达式方法 1、字符串中包含多个手机号码,代码如下:   2、字符串中只有一个手机号码,代码如下:   不用正则表达式实现 1、使用substr_replace字符串部分替换函数,代码如下:   2、使用字符串截取函数substr,代码如下: ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……