0

I'm having a problem with inserting previously defined PHP value $video_url as one of the array members. Here the code:

$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 */
    }
}

So I have something word by inserting $video_url into array. Thanks.

1 Answer 1

2

Try without quotes around $video_url:

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

Single quotes do not interpolate strings, meaning the value of $url[0] is literally $video_url.

Also, take care with comments, in PHP they are not #.

Sign up to request clarification or add additional context in comments.

9 Comments

Tried without quotes, the same result
Are you fix your comments? Remove # and add //. Can you var_dump($url) to know what is inside it?
I've fixed comments (and in my original post). Here is var_dump($url) result: array(2) { [0]=> string(17) "youtu.be/9bZkp7q19f0" [1]=> string(43) "youtube.com/watch?v=e-ORhEE9VVg" }
It works like a charm: regex101.com/r/kW7jM2/1 . Why the second URL have 43 characters?
have no idea about 43 characters
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.