请帮帮我,先生。如何使用php解析多个标签
<?php
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname ?.*>(.*)</$tagname>/";
preg_match($pattern, $string, $matches); return $matches[1];
}
$str = '<mp4>hxxps://test.com/vid1.mp4</mp4><mp4>hxxps://test.com/vid2.mp4</mp4><mp4>hxxps://test.com/vid3.mp4</mp4><mp4>hxxps://test.com/vid4.mp4</mp4><mp3>hxxps://test.com/music1.mp3</mp3><mp3>hxxps://test.com/music2.mp3</mp3>';
echo getTextBetweenTags($str, "mp4"); // hxxps://test.com/vid4.mp4
?>
如何从mp4中通过列表标记输出示例:
hxxps://test.com/vid1.mp4
hxxps://test.com/vid2.mp4
hxxps://test.com/vid3.mp4
hxxps://test.com/vid4.mp4
<?php
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<{$tagname}>(.*?)</{$tagname}>/";
preg_match_all($pattern, $string, $matches); return $matches[1];
}
$str = '<mp4>hxxps://test.com/vid1.mp4</mp4><mp4>hxxps://test.com/vid2.mp4</mp4><mp4>hxxps://test.com/vid3.mp4</mp4><mp4>hxxps://test.com/vid4.mp4</mp4><mp3>hxxps://test.com/music1.mp3</mp3><mp3>hxxps://test.com/music2.mp3</mp3>';
print_r(getTextBetweenTags($str, "mp4")); // hxxps://test.com/vid4.mp4
?>
结果
Array
(
[0] => hxxps://test.com/vid1.mp4
[1] => hxxps://test.com/vid2.mp4
[2] => hxxps://test.com/vid3.mp4
[3] => hxxps://test.com/vid4.mp4
)