两个php函数:
getsubstrbytag($str,$starttag,$endtag):指定一段文字,开始标记和结束标记,返回中间的字符。
function getsubstrbytagandtimes($str,$starttag,$endtag,$i):指定一段文字,开始标记和结束标记,返回该标记的第$i次出现的内容。
PHP代码
- function getsubstrbytag($str,$starttag,$endtag)
- {
- //根据函数开始标记和结束标记返回中间的字符串
- if(substr_count($str, $starttag)!=1||substr_count($str, $endtag)!=1)
- {
- //开始标记或者结束标记不唯一
- try
- {
- $tempstr=explode($starttag, $str);
- $tempstr=explode($endtag,$tempstr[1] );
- return $tempstr[0];
- }catch (Exception $e) {
- return 'tag error';
- }
- }
- else
- {
- $tempstr=explode($starttag, $str);
- $tempstr=explode($endtag,$tempstr[1] );
- return $tempstr[0];
- }
- }
- function getsubstrbytagandtimes($str,$starttag,$endtag,$i)
- {
- //根据函数开始标记和结束标记返回中间的字符串,$i表示第几次出现的
- if(substr_count($str, $starttag)!=1||substr_count($str, $endtag)!=1)
- {
- //开始标记或者结束标记不唯一
- try
- {
- $tempstr=explode($starttag, $str);
- $tempstr=explode($endtag,$tempstr[$i] );
- return $tempstr[0];
- }catch (Exception $e) {
- return 'tag error';
- }
- }
- else
- {
- $tempstr=explode($starttag, $str);
- $tempstr=explode($endtag,$tempstr[$i] );
- return $tempstr[0];
- }
- }
