如何从字符串输出中删除回车符? [英] How to remove carriage returns from output of string?

查看:86
本文介绍了如何从字符串输出中删除回车符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将wordpress用作CMS,并试图允许输入用户字段来填充Google Map脚本中的信息窗口。我使用它来选择id并从自定义字段中提取内容。



它工作正常,除非在自定义字段中存在任何打破脚本的html 。
我查看了htmlspcialchar和htmlentities,而不是去掉所有内容,我希望它能够逃脱,因此它仍然可以正常工作,并且html完好无损。有什么建议么?我对PHP很新,并且非常感谢任何指针。

过了一段时间后,我仍然无法找到一个很好的解决方案。 TheDeadMedic建议我使用esc_js



但打印所有实际的html代码而不是渲染它。



谢谢如果您的解决方案稍微好一些,但是如果输出中有回车符,则脚本仍然会中断,这对于CMS来说并不是那么好。



Something其他我试过的是使用修剪功能..这是我现在在哪里工作,只要在输出中没有\r。 $ snip字符串,mapExcerpt字段是返回信息的来源:

 <?php $ post_id = 207; // WordPress的帖子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> ;。$剪断。 '< / 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);

有什么其他想法可以解决这个问题?

$ b

  $ snip = str_replace('。','',$ snip); //删除点
$ snip = str_replace('','',$ snip); //删除空格
$ snip = str_replace(\t,'',$ snip); //删除标签
$ snip = str_replace(\\\
,'',$ snip); //删除新行
$ snip = str_replace(\r,'',$ snip); / /删除回车

或者一个解决方案:

  $ snip = str_replace(array('。','',\\\
,\ t,\r),'' ,$ snip);

您也可以使用正则表达式:

  $ snip = preg_replace('〜[[:cntrl:]]〜','',$ snip); //删除所有控制字符
$ snip = preg_replace('〜[。[:cntrl:]]〜','',$ snip); //上面的+ dots
$ snip = preg_replace('〜[。[:cntrl:] [:space:]]〜','',$ snip); //上面的空格

您仍然需要使用 addslashes() 在Javascript中输出 $ snip


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.

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.

After a while I am still unable to find a great solution for this. TheDeadMedic suggested I use esc_js

but that printed all of the actual html code instead of rendering it.

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.

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:

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

解决方案

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 $snip inside Javascript.

这篇关于如何从字符串输出中删除回车符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆