Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

-------------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.

share|improve this question
2  
possible duplicate of How to assign Php variable value to Javascript variable? – Rizier123 Feb 24 '15 at 15:47
    
Well you have to quote that string, but I can assure that you will need to more than just echo out the tweet. There will be quotes and junk in there that will cause JavaScript errors. – Halfstop Feb 24 '15 at 15:47
1  
you should use json_encode php function. See here stackoverflow.com/questions/168214/… – Vivek Feb 24 '15 at 15:49
up vote 0 down vote accepted

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>
share|improve this answer
    
And what if the string has a double-quote in it? It would come out as var e = 'He said, \"Yes, that\'s it.\"'; – Halfstop Feb 24 '15 at 16:20
    
Have fun removing slashes in your code. This is not the correct solution to your problem. – Halfstop Feb 24 '15 at 16:36
<script type="text/javascript">
    var e = '<?php echo $tweetText; ?>'; 
    var f = twemoji.parse(e);
    document.write(f);
</script>

That's it

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

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>
share|improve this answer

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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.