1
我想提出一个返回标签之间的内容(无论是整个字符串或者开始标签后的指定数量的字母) 线性代码如下功能:函数返回子和修剪串
$tag='<body>';
//case1
$source=substr($source,strpos($source,$tag)+strlen($tag));
$sub=substr($source,0,strpos($source,'<'));
//case2
$source=substr($source,strpos($source,$tag)+strlen($tag));
$sub=substr($source,0,3);
该函数将接受3个参数:源代码,指定的标记和子字符串长度(对于情况2)并将返回2个变量:修剪后的源和子字符串。所以basicaly我想有这样的功能:
function p($source,$tag,$len) {
$source=substr($source,strpos($source,$tag)+strlen($tag));
if(isset($len)) $sub=substr($source,0,$len);
else $sub=substr($source,0,strpos($source,'<'));
$ret=array();
$ret[0]=$source;
$ret[1]=$sub;
return $ret;
}
//
$source=p($source,'<strong>')[0];
$sub1=p($source,'<strong>')[1];
$source=p($source,'<p>',100)[0];
$sub2=p($source,'<p>',100)[1];
这是什么语言?请用该语言重新标记。 –
也许使用XML解析器? http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html –
@FrostyZ我不需要解析所有的代码,只需选择标签和1个函数就足够了。 – user965748