Home > Backend Development > PHP Tutorial > php正则截取字符串

php正则截取字符串

WBOY
Release: 2016-06-23 13:52:20
Original
1106 people have browsed it

字符串的格式是这个样子的
请问怎么截取src=“”里面的内容呢,也就是红字部分。求指导啊


回复讨论(解决方案)

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

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

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

成功了十分感谢,还想请教个问题,就是为什么$m数组里面会用两遍匹配的内容,还有就是字符串前的“<<

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

已成功,十分感谢,问一下最后的数组里为什么会出现三次结果

去看一下preg_match 的用法就明白了。

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

去看一下preg_match 的用法就明白了。

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

哦,谢谢了
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template