Rumah >pembangunan bahagian belakang >tutorial php >javascript - 怎么用php匹配出这段代码?
我想把下面代码里的/MusicData/play/201512/5684f44201b44.mp3这个给匹配出来
<code><a href="javascript:;" id="jplay_play" class="jp-play bp-play" title="play"></a> <a href="javascript:;" title="2016.01.01 搜浩连锁旗下蚂蚁88三亚店女DJVINA现场歌路" uid="38027" class="uid pause-item jp-pause_38027"></a> <a href="javascript:;" title="2016.01.01 搜浩连锁旗下蚂蚁88三亚店女DJVINA现场歌路" uid="38027" timename="201512" crimp="1" class="uid play-item jp-play_38027 foradd" url="/MusicData/play/201512/5684f44201b44.mp3"></a> <div class="commentRoll hide"> oooovc: <span style="color:#01aebf;">终于有更新的曲子了!等得花都谢了</span> DJHarlem: <span style="color:#01aebf;">不错的曲子 值得拥有~</span> NONGZEMING: <span style="color:#01aebf;">不错!我喜欢!</span> NONGZEMING: <span style="color:#01aebf;">不错!我喜欢!</span> xx602285388: <span style="color:#01aebf;">套曲不错~~~</span> 155774a: <span style="color:#01aebf;">感谢分享好音乐</span> 1098263279: <span style="color:#01aebf;">非常不错的曲子</span> 376663234: <span style="color:#01aebf;">霸气 不错的套曲 支持</span> 936431866dj: <span style="color:#01aebf;">不错,赞一个</span> a670529347: <span style="color:#01aebf;">嗨嗨嗨嗨嗨~</span> <h2>回复内容:</h2> <p>我想把下面代码里的/MusicData/play/201512/5684f44201b44.mp3这个给匹配出来</p> <pre class="brush:php;toolbar:false"><code><a href="javascript:;" id="jplay_play" class="jp-play bp-play" title="play"></a> <a href="javascript:;" title="2016.01.01 搜浩连锁旗下蚂蚁88三亚店女DJVINA现场歌路" uid="38027" class="uid pause-item jp-pause_38027"></a> <a href="javascript:;" title="2016.01.01 搜浩连锁旗下蚂蚁88三亚店女DJVINA现场歌路" uid="38027" timename="201512" crimp="1" class="uid play-item jp-play_38027 foradd" url="/MusicData/play/201512/5684f44201b44.mp3"></a> <div class="commentRoll hide"> oooovc: <span style="color:#01aebf;">终于有更新的曲子了!等得花都谢了</span> DJHarlem: <span style="color:#01aebf;">不错的曲子 值得拥有~</span> NONGZEMING: <span style="color:#01aebf;">不错!我喜欢!</span> NONGZEMING: <span style="color:#01aebf;">不错!我喜欢!</span> xx602285388: <span style="color:#01aebf;">套曲不错~~~</span> 155774a: <span style="color:#01aebf;">感谢分享好音乐</span> 1098263279: <span style="color:#01aebf;">非常不错的曲子</span> 376663234: <span style="color:#01aebf;">霸气 不错的套曲 支持</span> 936431866dj: <span style="color:#01aebf;">不错,赞一个</span> a670529347: <span style="color:#01aebf;">嗨嗨嗨嗨嗨~</span> <p class="answer fmt" data-id="1020000004234550"> </p> <p><img data-src="/img/bVrVLl" alt="javascript - 怎么用php匹配出这段代码?" ></p> <pre class="brush:php;toolbar:false"><code><?php $a = <<<EOF <a href="javascript:;" id="jplay_play" class="jp-play bp-play" title="play"> <a href="javascript:;" title="2016.01.01 搜浩连锁旗下蚂蚁88三亚店女DJVINA现场歌路" uid="38027" class="uid pause-item jp-pause_38027"></a> <a href="javascript:;" title="2016.01.01 搜浩连锁旗下蚂蚁88三亚店女DJVINA现场歌路" uid="38027" timename="201512" crimp="1" class="uid play-item jp-play_38027 foradd" url="/MusicData/play/201512/5684f44201b44.mp3"></a> <div class="commentRoll hide"> oooovc: <span style="color:#01aebf;">终于有更新的曲子了!等得花都谢了</span> DJHarlem: <span style="color:#01aebf;">不错的曲子 值得拥有~</span> NONGZEMING: <span style="color:#01aebf;">不错!我喜欢!</span> NONGZEMING: <span style="color:#01aebf;">不错!我喜欢!</span> xx602285388: <span style="color:#01aebf;">套曲不错~~~</span> 155774a: <span style="color:#01aebf;">感谢分享好音乐</span> 1098263279: <span style="color:#01aebf;">非常不错的曲子</span> 376663234: <span style="color:#01aebf;">霸气 不错的套曲 支持</span> 936431866dj: <span style="color:#01aebf;">不错,赞一个</span> a670529347: <span style="color:#01aebf;">嗨嗨嗨嗨嗨~</span> EOF; preg_match('/ url="(.+?)">/', $a, $b); var_dump($b); <p class="answer fmt" data-id="1020000004236704"> </p> <blockquote><ul> <li><p>用PHP的XML的XPath最好了</p></li> <li><p>XPath语法参加这里XPath语法</p></li> </ul></blockquote> <p><b>这里有一个小例子</b></p> <pre class="brush:php;toolbar:false"><code class="PHP"><?php $xml = simplexml_load_file("test.xml"); $result = $xml->xpath("//a[@class='play-item' and @url]"); print_r($result); ?></code>
我们可以找到所有a标签class属性为'play-item'且有url属性的元素,然后就是对PHP中SimpleXMLElement类的操作了