1

In my PHP application I am displaying embedded videos from various sources - youtube, vimeo etc.

In my articles table I store the id of my embed videos:

Article.video_url = 'e5dkjwkdek'

Then in my video_providers table I store the embed code, but I want to dynamically add the video_url, so I store:

VideoProviders.embedcode = "https://www.youtube.com/embed/$article->video_url"

I then output the 'embedcode' variable in my template and I want to append the $article->video_url which is the unique id of the video.

It seems I need a variable within the variable, I tried:

VideoProviders.embedcode = "https://www.youtube.com/embed/{$article->video_url}"

and

VideoProviders.embedcode = "https://www.youtube.com/embed/$$article->video_url"

But it seems these are simply treated as literal strings. If anyone has any better suggestions as to how to achieve this I am all ears. In short I want the mark-up stored in one place so that is can be edited should the embed code of the provider change.

3
  • 1
    Where does this code exist? Why does it look like you're mixing js and php? Commented Aug 29, 2018 at 23:47
  • 1
    the left side is supposed to represent the table and its field, so "embedcode" is the field of the VideoProviders table Commented Aug 29, 2018 at 23:52
  • So you're storing a literal $article in your database and you want it evaluated when you echo that string? Commented Aug 29, 2018 at 23:54

2 Answers 2

2

You cannot store strings with PHP interpolation symbols. Double-quoted strings are interpolated at runtime.

I'd say you have three options...

  1. Store the various parts in your database and construct them in your query. For example (assuming MySQL)

    SELECT
      CONCAT(VideoProviders.embed_prefix, Article.video_url) AS embedcode,
      -- etc
    

    with VideoProviders.embed_prefix containing 'https://www.youtube.com/embed/', 'https://player.vimeo.com/video/', etc

  2. Store the URL with placeholders for the video id and (again assuming MySQL) use the REPLACE function. For example

    VideoProviders.embedcode = 'https://www.youtube.com/embed/{VIDEO_ID}'
    

    and

    SELECT
      REPLACE(VideoProviders.embedcode, '{VIDEO_ID}', Article.video_url) AS embedcode,
      -- etc
    

    This would be the preferred solution if the video id doesn't always appear at the end of the URL.

  3. Store a printf style pattern in embedcode, eg 'https://www.youtube.com/embed/%s' and put it together with PHP, eg

    $embedcode = sprintf($row['embedcode'], $row['video_url');
    
Sign up to request clarification or add additional context in comments.

2 Comments

the 3rd suggestion does exactly what I need - sprintf
That's sort of odd. It's really no different to option #2
0

I think you can just do this:

$video_id = 'e5dkjwkdek';
$provider_url = 'https://www.youtube.com/embed/';

$embed_code = $provider_url . $video_id;

Or something similar to this. You can fill the video_id and provider_url anyway you like. From a database or from some other source. This way should always work.

1 Comment

this would have worked but there are more parts to the mark-up which would have meant breaking the embed code up into the before and after sections which I think could get messy

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.