Home  >  Article  >  Backend Development  >  php正则截取字符串,该怎么处理

php正则截取字符串,该怎么处理

WBOY
WBOYOriginal
2016-06-13 12:04:011330browse

php正则截取字符串
字符串的格式是/fofa/upload/2014-08/05/5.mp4" type="video/x-ms-asf-plugin" width="550" height="400" autostart="false" loop="true" />这个样子的
请问怎么截取src=“”里面的内容呢,也就是红字部分。求指导啊
------解决方案--------------------

$s=<<<txt<br /><embed src="/fofa/upload/2014-08/05/5.mp4" type="video/x-ms-asf-plugin" width="550" height="400" autostart="false" loop="true" /><br />txt;<br />preg_match('/src="(.+?)"/i', $s, $m);<br />echo $m[1];

------解决方案--------------------
$s =<<< TXT<br />字符串的格式是<embed src="/fofa/upload/2014-08/05/5.mp4" type="video/x-ms-asf-plugin" width="550" height="400" autostart="false" loop="true" />这个样子的<br />请问怎么截取src="..."里面的内容呢,也就是红字部分。求指导啊<br />TXT;<br />preg_match_all('/src=([\'"]?)(.+?)\1/is', $s, $m);<br />print_r($m);<br />
Array<br />(<br />    [0] => Array<br />        (<br />            [0] => src="/fofa/upload/2014-08/05/5.mp4"<br />            [1] => src="..."<br />        )<br /><br />    [1] => Array<br />        (<br />            [0] => "<br />            [1] => "<br />        )<br /><br />    [2] => Array<br />        (<br />            [0] => /fofa/upload/2014-08/05/5.mp4<br />            [1] => ...<br />        )<br /><br />)<br /><br />

------解决方案--------------------
去看一下preg_match 的用法就明白了。

可以看这里用法:http://php.net/manual/zh/language.types.string.php#language.types.string.syntax.heredoc

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