Javascript 如何从字符串的输出中删除回车符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3059091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove carriage returns from output of string?
提问by Zac
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.
我正在使用 wordpress 作为 CMS,并尝试允许输入用户字段以填充 Google Map 脚本中的信息窗口。我正在使用它来选择 id 并从自定义字段中提取内容。
It works fine unless there is any html in the custom-field which breaks the script.
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.
除非自定义字段中有任何 html 会破坏脚本,否则它可以正常工作。
我查看了 htmlspcialchar 和 htmlentities,但我不想删除所有内容,而是希望将其转义,以便它仍然有效并且 html 完好无损。有什么建议?我对 PHP 很陌生,非常感谢任何指针。
After a while I am still unable to find a great solution for this. TheDeadMedic suggested I use esc_js
一段时间后,我仍然无法为此找到一个很好的解决方案。TheDeadMedic 建议我使用 esc_js
but that printed all of the actual html code instead of rendering it.
但这打印了所有实际的 html 代码而不是渲染它。
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.
感谢 nickfs,因为该解决方案稍微好一些,但是如果输出中有任何回车,脚本仍然会中断,这使得这对于 CMS 来说不是那么好。
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:
我尝试过的其他方法是使用修剪功能..这就是我现在所处的位置,只要输出中没有 \r 即可。$snip 字符串,mapExcerpt 字段是返回的来源:
<?php $post_id = 207; // Wordpress Post ID
$my_post = get_post($post_id);
$mapTitle = $my_post->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 = '<div class="span-8"><div class="mapTitle">'.$mapTitle.'</div><div class="mapContent">'.$snip.'</div></div>';
$trimmed = trim($pass_to, " \r.");
?>
var point = new GLatLng('<?php echo $lat; $lat; ?>','<?php echo $long; $long; ?>');
var marker = createMarker(point,"<?php echo $mapTitle; $mapTitle; ?>", '<?php echo addslashes($trimmed); ?>');
map.addOverlay(marker);
Any other ideas out there on how I can pull this off?
关于如何解决这个问题的任何其他想法?
回答by Alix Axel
I don't fully understand your exact problem, but the answer to the title of your question is quite simple:
我不完全理解你的确切问题,但你问题标题的答案很简单:
$snip = str_replace('.', '', $snip); // remove dots
$snip = str_replace(' ', '', $snip); // remove spaces
$snip = str_replace("\t", '', $snip); // remove tabs
$snip = str_replace("\n", '', $snip); // remove new lines
$snip = str_replace("\r", '', $snip); // remove carriage returns
Or a all in one solution:
或多合一解决方案:
$snip = str_replace(array('.', ' ', "\n", "\t", "\r"), '', $snip);
You can also use regular expressions:
您还可以使用正则表达式:
$snip = preg_replace('~[[:cntrl:]]~', '', $snip); // remove all control chars
$snip = preg_replace('~[.[:cntrl:]]~', '', $snip); // above + dots
$snip = preg_replace('~[.[:cntrl:][:space:]]~', '', $snip); // above + spaces
You'll still need to use addslashes()to output $snipinside Javascript.
您仍然需要使用addslashes()来$snip在 Javascript 中输出。
回答by Tim
I always use this to get rid of pesky carriage returns:
我总是用它来摆脱讨厌的回车:
$string = str_replace("\r\n", "\n", $string); // windows -> unix
$string = str_replace("\r", "\n", $string); // remaining -> unix
回答by annapp
if you need to remove new line symbols of all types (in utf8)
如果您需要删除所有类型的新行符号(在 utf8 中)
$dataWithoutCarrierReturn = preg_replace('/\R/', '', $inData);
回答by Alisso
This was the only thing solution that worked for me:
这是唯一对我有用的解决方案:
$dataWithoutCarrierReturn = preg_replace('/[\x0D]/', '', $inData);
回答by nickf
Since you're putting this into Javascript, you'll need to escape it for javascript strings. addslashes()should do the trick.
由于您将其放入 Javascript,因此您需要将其转义为 javascript 字符串。addslashes()应该做的伎俩。
回答by Jan Fabry
Try JSON-encoding it, I always do that when I send data from PHP to Javascript. It solves most encoding issues, including newlines.
尝试对它进行 JSON 编码,当我将数据从 PHP 发送到 Javascript 时,我总是这样做。它解决了大多数编码问题,包括换行符。
回答by TheDeadMedic
Use WP's own esc_js(), which will escape quotes and line breaks for JavaScript strings.
使用 WP 自己的esc_js(),它将转义 JavaScript 字符串的引号和换行符。

