将PHP值作为数组成员插入?

时间:2016-06-22 16:40:29

标签: php arrays

我在插入以前定义的PHP值$ video_url作为其中一个数组成员时出现问题。代码如下:

$video_url= "[[+virtual_tour]]"; /* [[+virtual_tour]] is MODX placeholder which contains first video url */

echo $video_url; /* for test reasons. Outputs https://youtu.be/9bZkp7q19f0 */

$url = array(
'$video_url', /* URL of the first video */
'https://www.youtube.com/watch?v=e-ORhEE9VVg' /*URL of the second video */
);
// Extracts the YouTube ID from various URL structures
foreach ($url as $value) {
    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $value, $match)) {
        $id = $match[1];
echo $id; /* outputs only ID of second video, but not first */
    }
}

所以我通过将$ video_url插入数组来获得一些单词。感谢。

1 个答案:

答案 0 :(得分:2)

$video_url附近试用不带引号:

$url = array(
   $video_url, /*URL of the first video**/
   'https://www.youtube.com/watch?v=e-ORhEE9VVg' /* URL of the second video */
);

单引号do not interpolate字符串,表示$url[0]的值字面上为$video_url

另外,请注意注释,在PHP中它们不是#