title: Symfony: escape single quote string from database?
body: <p>I need to generate a link with a Javascript confirmation dialog using Symfony's <code>link_to()</code> method. The confirmation dialog text gets some of it's content from a database entry:</p>
  
  <pre><code>&lt;?php echo link_to( "click here", 'category/delete?id='.$id, array( 'confirm' =&gt; 'Are you sure you want to delete the category: '.$category-&gt;getName().'?' ) ) ?&gt;
  </code></pre>
  
  <p>But if the database entry has a <code>single quote</code> in it, the confirm dialog doesn't work because the generated JS is surrounded with single quotes. So if I have a category called "John's Articles", the generated JS starts like this:</p>
  
  <pre><code>&lt;a onclick="if (confirm('Are you sure you want to delete the category: John's Articles?')) { var f = document.createElement('form'); f.styl.... etc... "
  </code></pre>
  
  <p>So, the single quote in there screws up the confirmation, etc...</p>
  
  <p>Anyways I thought I would simply run <code>$category-&gt;getName()</code> through <code>addslashes()</code> but it didn't add any slashes... I also tried saving out the category name as a separate variable ahead of time and adding slashes to that. But it didn't add any. Then I started looking at Symfony's <a href="http://www.symfony-project.org/api/1_4/EscapingHelper" rel="nofollow">escaping methods</a> and found methods like <code>esc_entities()</code> but they resulted in the text looking like <code>John&amp;amp;#039;s Articles</code>.</p>
  
  <p>What do I do? All I want to do is add in a single slash before single quotes in that string. I never tried <code>str_replace("'","\'",$category-&gt;getName())</code> but THAT didn't even do anything. I can create my own basic string in my template like <code>Alex's Test</code> and <code>addslashes()</code> to it just fine. It's just this value from the database that I can't add any slashes to.</p>
  
  <p>When I look at the value in the database, it looks just like <code>John's Articles</code>. There are no special characters or encoded characters.</p>
  
  <p>What am I missing here?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've tried the following code with the following results:</p>
  
  <pre><code>echo $category-&gt;getName()."&lt;br/&gt;";
  
  echo addslashes($category-&gt;getName())."&lt;br/&gt;";
  
  $tmp = $category-&gt;getName();
  echo addslashes($tmp)."&lt;br/&gt;";
  
  $tmp = addslashes($category-&gt;getName());
  echo $tmp."&lt;br/&gt;";
  
  $tmp = "Testing's the Testing";
  echo addslashes($tmp)."&lt;br/&gt;";
  
  $tmp = str_replace("'","\\'",$category-&gt;getName());
  echo $tmp;
  </code></pre>
  
  <p>Results:</p>
  
  <pre>
  John's Articles
  John's Articles
  John's Articles
  John's Articles
  Testing\'s the Testing
  John's Articles
  </pre>
  
  <p>The values from the database simply will not get slashes added to them...</p>