0

I'm trying to figure out how i can use quotes in a third "layer".
Do i need to "escape" the quotes somehow?

PHP/JS/HTML

echo "<script type='text/javascript'>";
    echo "$('#rImgObjNr').text('Bilder: ".$imgDirs[$randomDir]."<i class='fa'></i>');";
echo "</script>";

It's my icon element that has problems with it's quotes:
<i class='fa'></i>

When i use 'I "close" JS text.. and if i use " i close the PHP.
How can i solve this?
Can i use my HTML element inside of this PHP/JS text?

4
  • 1
    To escape quotes and achieve a nested quotes just use \" as in "your\"string\"" which would output your"string". This works in Php and Js Commented Jul 1, 2016 at 10:11
  • Try escaping like <i class=\'fa\'> Commented Jul 1, 2016 at 10:12
  • ok, it worked, but it wasn't the whole answear. I had to change .textto .htmlalso. Commented Jul 1, 2016 at 10:23
  • Try next time with heredoc. I think is easier with nested quotes. Commented Jul 1, 2016 at 10:30

3 Answers 3

2

You need to escape your double quotes by prepending backslash. Replace your code like this below.

echo "<script type='text/javascript'>";
    echo "$('#rImgObjNr').text('Bilder: ".$imgDirs[$randomDir]."<i class=\"fa\"></i>');";
echo "</script>";
Sign up to request clarification or add additional context in comments.

Comments

1

May be it work :

<script type='text/javascript'>
    $('#rImgObjNr').text("Bilder: <?php echo $imgDirs[$randomDir];?><i class='fa'></i>");
</script>

Comments

0

The solution was to change .textto .html and then escape the quotes like \".

CODE

echo "<script type='text/javascript'>";
  echo "$('#rImgObjNr').html('Bilder: ".$imgDirs[$randomDir]."<i class=\"fa\"></i>');";
echo "</script>"; 

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.