title: How to remove carriage returns from output of string?
body: <p>I am using wordpress as a CMS and trying to allow user fields to be input to populate the info windows in a Google Map script.  I am using this to select the id and pull in the content from a custom field.</p>
  
  <p>It works fine unless there is any html in the custom-field which breaks the script.<br>
  I looked at htmlspcialchar and htmlentities but rather than strip everything out I would like to have it escaped so it still works and the html is intact.  Any suggestions?  I am pretty new to PHP and would really appreciate any pointers. </p>
  
  <p>After a while I am still unable to find a great solution for this.  TheDeadMedic suggested I use esc_js </p>
  
  <p>but that printed all of the actual html code instead of rendering it.</p>
  
  <p>Thank you to nickfs as that solution was slightly better but the script still breaks if there are any carriage returns in the output, which makes this not so great for a CMS.</p>
  
  <p>Something else I tried was to use the trim function.. this is where I am at now where it works as long as no \r in the output.  The $snip string, mapExcerpt field is where the returns are coming from:</p>
  
  <pre><code>&lt;?php $post_id = 207; // Wordpress Post ID
  $my_post = get_post($post_id);
  $mapTitle  = $my_post-&gt;post_title;
  $mapIMG = get_post_meta($post_id, 'mapImage', true);
  $snip = get_post_meta($post_id, 'mapExcerpt', true);
  $lat = get_post_meta($post_id, 'lat', true);
  $long = get_post_meta($post_id, 'long', true);
  $pass_to = '&lt;div class="span-8"&gt;&lt;div class="mapTitle"&gt;'.$mapTitle.'&lt;/div&gt;&lt;div class="mapContent"&gt;'.$snip.'&lt;/div&gt;&lt;/div&gt;';
  $trimmed = trim($pass_to, " \r.");
  ?&gt;
  var point = new GLatLng('&lt;?php echo $lat; $lat; ?&gt;','&lt;?php echo $long; $long; ?&gt;');
  var marker = createMarker(point,"&lt;?php echo $mapTitle; $mapTitle; ?&gt;", '&lt;?php echo addslashes($trimmed); ?&gt;');
  map.addOverlay(marker);
  </code></pre>
  
  <p>Any other ideas out there on how I can pull this off?</p>