0

我的正则表达式非常非常弱,我需要帮助编写 [并希望理解] php 正则表达式以在资源的内容字段中查找 modx 片段调用并提取该调用的属性之一。调用将如下所示:

[[ShowVideo? &path=`path/to/somevideo.mp4` &w=`960`&h=`540` &playlist=`true`]]

或可能称为未缓存

[[!ShowVideo? &path=`path/to/somevideo.mp4` &w=`960`&h=`540` &playlist=`true`]]

我需要从中提取的是路径属性中的“path/to/somevideo.mp4”值。

我怎样才能做到这一点?

编辑:

感谢您的帮助-如果有人感兴趣,这是完整的代码段:

<?php

$rid = $modx->resource->get('id'); // this resource id

$children = $modx->getChildIds($rid,1); // get one level of child ids

$pattern = '~\[\[!?ShowVideo[^\]]*&?path=`([^`]*)`~'; // thanks Crayon Violent

$myPlaylist = ''; // output to placeholder

$listItems = ''; // list thumbs with link

$index = 1; // not actually used

$parents = $modx->getParentIds($rid); // get the parent id array

if(!$children){

    // try siblings 
    $children = $modx->getChildIds($parents[0]); // get children of immediate parent

    array_unshift($children, $parents[0]); // prepend immediate parent id

}else{

    // must be a parent folder?
    array_unshift($children, $rid); // prepend this id

}

$videos = $children; // just for readbility

foreach($videos as $video){

    $resource = $modx->getObject('modResource', $video); // get the first resource object

    if ($resource->get('id') == $rid){ // figure out if this is the current video [to add a css class]

        $current = ' current';

    }else{

        $current = '';

    }

    if($resource){ 

        $rescontent = $resource->getContent(); // get the unprocessed! content field

        if(preg_match($pattern, $rescontent, $matches)){ // thanks again crayon!

            $listItems .= $modx->getChunk('PlayListItems', array( // get the output chunk & populate it's placeholders

                'videopath' =>  $modx->makeUrl($resource->get('id')),
                'thumbnail' => $resource->getTVValue('PageThumbnail'),
                'current' => $current,

            ));

        }

    }

    $index++; // again ~ not used

}


$myPlaylist = $modx->getChunk('PlayListWrapper', array( // get and populate the wrapper chunk

    'ListItems' =>  $listItems,

));


return $myPlaylist; // output! 
4

2 回答 2

3
preg_match('~&path=`([^`]*)`~',$string,$match);
// $match[1] will have the value

如果它需要更明确一点:

preg_match('~\[\[!?ShowVideo[^\]]*&path=`([^`]*)`~',$string,$match);
// $match[1] will have the value
于 2013-05-24T14:31:29.163 回答
2

不要把事情复杂化,保持简单:

preg_match('#path=`([^`]+)#', $input, $matches);

工作演示

于 2013-05-24T14:33:13.603 回答