This is 赵峰 | 赵峰之城 | 赵峰的博客 !

类:给定开始标记和结束标记,返回中间字符

两个php函数:

getsubstrbytag($str,$starttag,$endtag):指定一段文字,开始标记和结束标记,返回中间的字符。

function getsubstrbytagandtimes($str,$starttag,$endtag,$i):指定一段文字,开始标记和结束标记,返回该标记的第$i次出现的内容。

 

PHP代码
  1. function getsubstrbytag($str,$starttag,$endtag)       
  2. {       
  3.     //根据函数开始标记和结束标记返回中间的字符串       
  4.     if(substr_count($str$starttag)!=1||substr_count($str$endtag)!=1)       
  5.     {       
  6.         //开始标记或者结束标记不唯一       
  7.         try       
  8.         {       
  9.             $tempstr=explode($starttag$str);       
  10.             $tempstr=explode($endtag,$tempstr[1] );       
  11.             return $tempstr[0];       
  12.         }catch (Exception $e) {       
  13.             return 'tag error';       
  14.         }       
  15.     }       
  16.     else      
  17.     {       
  18.         $tempstr=explode($starttag$str);       
  19.         $tempstr=explode($endtag,$tempstr[1] );       
  20.         return $tempstr[0];       
  21.     }       
  22. }       
  23.       
  24. function getsubstrbytagandtimes($str,$starttag,$endtag,$i)       
  25. {       
  26.       
  27.       
  28.     //根据函数开始标记和结束标记返回中间的字符串,$i表示第几次出现的       
  29.     if(substr_count($str$starttag)!=1||substr_count($str$endtag)!=1)       
  30.     {       
  31.         //开始标记或者结束标记不唯一       
  32.         try       
  33.         {       
  34.             $tempstr=explode($starttag$str);       
  35.             $tempstr=explode($endtag,$tempstr[$i] );       
  36.             return $tempstr[0];       
  37.         }catch (Exception $e) {       
  38.             return 'tag error';       
  39.         }       
  40.     }       
  41.     else      
  42.     {       
  43.         $tempstr=explode($starttag$str);       
  44.         $tempstr=explode($endtag,$tempstr[$i] );       
  45.         return $tempstr[0];       
  46.     }       
  47. }      

« 上一篇 | 下一篇 »