Home  >  Article  >  Backend Development  >  求截取PHP指定字符串代码,该怎么解决

求截取PHP指定字符串代码,该怎么解决

WBOY
WBOYOriginal
2016-06-13 11:52:41830browse

求截取PHP指定字符串代码
有下面一段html字符串:

aaaa
bbbb
cccc
dddd
 >
要想输出下面内容:
aaaa
dddd
 >
即删除中间两个
前面的内容及
的简易代码,由于还要输出为html,不用数组方式。
------解决方案--------------------
$s = '<td align="Center" rowspan="2" width="7%">aaaa<br>bbbb<br>cccc<br>dddd<br></td >';<br />echo preg_replace('/<br>\w+/', '', $s, 2);
<td align="Center" rowspan="2" width="7%">aaaa<br>dddd<br></td ><br />

------解决方案--------------------
1
echo preg_replace('/<br>[^<]+/', '', $s, 2);


2
echo preg_replace('/\(.+?\)/', '', $s);

------解决方案--------------------
echo preg_replace_callback('/<br>[^<]+/', 'foo', $s, 2); <br />function foo($m){<br />     return strpos($m[0], '(') !== false ? preg_replace('/\(.+?\)/','',$m[0]) : '';	    	 <br />}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn