播放列表.xml自定义播放列表路径问题


Playlist.xml custom playlist Path Issue

>好的,基本上我遇到的问题是我有一个名为 Playlist.xml 的文件,其中包含我的 rtmp 流路径,但是我需要根据正在播放的房间更改流,因此在加载时它会编辑流的路径,例如在 PHP 中,我会使用 _GET 函数来执行此操作,然后在 rtmp 路径中回显出用户名,但是我有问题是Playlist.xml加载到.swf file中并且文档未.php,那么对于获取我所在个人资料的用户名.xml file,我该怎么办。

播放列表.xml

<?xml version="1.0" encoding="UTF-8"?>
<playlist>
  <playitem caption="username" path="rtmp://111.11.11.111/live/username" image="FirstFrame.jpg" options="" clickurl="" clicktarget="_blank" endurl="" styleoftarget="browser" endtarget="">
    <watermarkitem position="00:00:00:000" duration="00:00:00:000" videotype="image" filepath="/logo.png" clickurl="" clicktarget="_blank" fadein="0" fadeout="0" origin="top-left" offsetleft="10" offsettop="11" width="200" height="20" transparency="94" options=""/>
  </playitem>
</playlist>

因此,问题出在文档的这一部分;

path="rtmp://111.11.11.111/live/username" 

我需要username拥有我正在查看的房间的用户名,这将如何实现?谢谢。

为此使用DOMDocumentDOMXpathpreg_replace()

首先,init DOMDocument ,加载你的 XML 并初始化DOMXPath

$dom = new DOMDocument();
$dom->loadXML( $xml );
$xpath = new DOMXPath( $dom );

然后,选择playitem节点的所有path属性:

$nodes = $xpath->query( '//playlist/playitem/@path' );

最后,处理每个节点,并通过preg_replace()调用将用户名添加到数组中:

$usernames = array();
foreach( $nodes as $node )
{
    $usernames[] = preg_replace( '{^.+/([^/]+)$}',''1',$node->nodeValue );
}

现在,在$usernames数组中,您拥有所有用户名。

3v4l 演示

正则表达式说明:

^           Start of string
.+          zero-or-more undefined characters
/           a slash
([^/]+)     group 1: one-or-more not-slash characters
$           End of string

  • 查看更多关于 DOMDocument 的信息
  • 查看更多 关于 DOMXpath
  • 查看更多关于preg_replace