-5

I have this string in php

$tagged

and I want to put it in my javacript script inside the ' '

tagName: '',

how do I do this

Sergio
  • 27,998
  • 10
  • 81
  • 130
user1616846
  • 181
  • 1
  • 1
  • 9

2 Answers2

1

The best way IMHO would be this:

tagName: <?php echo json_encode($tagged); ?>

This way, you don't have to take care of escaping quotes, escape characters or other unwanted signs.

ComFreek
  • 28,220
  • 17
  • 99
  • 151
1

If your view is rendered by PHP:

tagName: '<?= json_encode($tagged)?>',

If your JS code is a part of a PHP view, build the object from PHP:

$jsObject = json_encode(array('tagName' => $tagged'));
Eirik Hoem
  • 1,310
  • 7
  • 14