0

I have the following script in php:

<?php

$my_array_of_vars['v'];

    $row['comment'] = "<img src='http://img.youtube.com/vi/xxxxxx/hqdefault.jpg' title='YouTube Video' alt='YouTube Video' /> ";

?>

All I want is to replace the "xxxxxx" with the result in variable $my_array_of_vars['v']; I have tried a lot of things but always getting wrong result. Any idea how to do this please?

This is what I have tried:

<?php
    $row['comment'] = "<img src='http://img.youtube.com/vi/$my_array_of_vars['v']/hqdefault.jpg' title='YouTube Video' alt='YouTube Video' /> ";
?>
2
  • Did you try just regular concantenation with closing the quotes and periods. Commented Feb 1, 2014 at 14:59
  • What are you trying to do in the end? For many small problems like this one, lots of different ways exist and giving the context of your question helps finding the best appropriate answer. Commented Feb 1, 2014 at 15:02

3 Answers 3

2

You could use those fancy brackets {} like this:

$row['comment'] = "<img src='http://img.youtube.com/vi/{$my_array_of_vars['v']}/hqdefault.jpg' title='YouTube Video' alt='YouTube Video' /> ";

or properly concatenate the variable to the string:

$row['comment'] = "<img src='http://img.youtube.com/vi/".$my_array_of_vars['v']."/hqdefault.jpg' title='YouTube Video' alt='YouTube Video' /> ";
Sign up to request clarification or add additional context in comments.

1 Comment

Also quotes around array keys are not required with "fancy" brackets.
0

you can use like this:

     $row['comment'] = "<img src='http://img.youtube.com/vi/".$my_array_of_vars['v']."/hqdefault.jpg' title='YouTube Video' alt='YouTube Video' /> ";

Comments

0

You must use urlencode(). Here is best way:

$row['comment'] = "<img src='http://img.youtube.com/vi/".urlencode($my_array_of_vars['v'])."/hqdefault.jpg' title='YouTube Video' alt='YouTube Video' /> ";

Comments

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.