Reputation: 2551
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.
Upvotes: 1
Views: 67
Reputation: 164728
You cannot store strings with PHP interpolation symbols. Double-quoted strings are interpolated at runtime.
I'd say you have three options...
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
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.
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');
Upvotes: 2
Reputation: 110
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.
Upvotes: 0