0

-------------php code ends here-------------

<script type="text/javascript">
    var e = <?php echo $tweetText; ?>; 
    var f = twemoji.parse(e);
    document.write(f);
</script>

-------------php code starts here-------------

If I put single quotes or double quotes before and after then it matches any ' or " inside the tweetText and doesn't let the string print to screen.

3

4 Answers 4

0

This solution works fine, just add ,,addslashes" to escape quotes

<script type="text/javascript">
    var e = '<?php echo addslashes($tweetText); ?>'; 
    var f = twemoji.parse(e);
    document.write(f);
</script>
2
  • And what if the string has a double-quote in it? It would come out as var e = 'He said, \"Yes, that\'s it.\"'; Commented Feb 24, 2015 at 16:20
  • Have fun removing slashes in your code. This is not the correct solution to your problem. Commented Feb 24, 2015 at 16:36
0
<script type="text/javascript">
    var e = '<?php echo $tweetText; ?>'; 
    var f = twemoji.parse(e);
    document.write(f);
</script>

That's it

4
  • And if the Tweet looks like this: I love that you're a Leo. Commented Feb 24, 2015 at 15:52
  • can you explain your comment please @Halfstop? Commented Feb 24, 2015 at 15:53
  • I tried and this works fine, I don't understend what is the problem and why I get minus? Commented Feb 24, 2015 at 15:54
  • Does this look like valid JS? var e = 'I love that you're a Lew.'; Commented Feb 24, 2015 at 16:18
0

The best way to do this would be with json_encode as @vivek said in a comment.

<script type="text/javascript">
var e = <?php echo json_encode($tweetText); ?>;
var f = twemoji.parse(e);
document.write(f);
</script>
-1

Try to put the whole string inside doublequotes, then escape the same - double quotes - inside the javascript string, with a simple str function:

<script type="text/javascript">
var e = "<?php echo str_replace('"','\"',$tweetText); ?>"; 
var f = twemoji.parse(e);
document.write(f);
</script>

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.