84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
用js和php分别去掉表单首尾的空格和多余的换行怎么做?
比如stackoverflow提问时的编辑器,它可以:1、提交问题的时候,如果有很多个换行,问题提交后显示的时候只有一个换行。2、去掉首尾空格。
问题:1、js在前端去掉,后台php还要做一次同样的事情,所以分别怎么做呢? 2、有没有像stackoverflow那样的编辑器推荐?
欢迎选择我的课程,让我们一起见证您的进步~~
1、js在前端去掉,后台php还要做一次同样的事情,所以分别怎么做呢?
JS用字符串的trim()方法,换行、tab也会一并去除,IE8前要打填充,连结里有。例子:
var orig = ' foo '; var new = orig.trim(); // 'foo'
不过trim只能去除字符串的前后换行,如果你要去除在字符串夹在中间的换行,要用下面的正则式,出自这里的回答:
Var someText = "Here's some text.\n It has some line breaks \r using Javascript.\r\n"; someText = someText.replace(/(\r\n|\n|\r)/gm,"");
PHP也有trim()函数可用,一样是去除空白、tab与字符串的前后换行,例子如下:
$text = "\t\tThese are a few words :) ... "; $trimmed = trim($text);
去除字符串的夹在中间的换行也是用正则式,出自这里的回答:
$string = trim(preg_replace('/\s\s+/', ' ', $string));
2、有没有像stackoverflow那样的编辑器推荐?
https://github.com/pandao/edi...
http://markitup.jaysalvat.com...
http://www.codingdrama.com/bo...
js正则"xxx".replace(/(^s)|(s$)/g, "");
JS用字符串的trim()方法,换行、tab也会一并去除,IE8前要打填充,连结里有。例子:
不过trim只能去除字符串的前后换行,如果你要去除在字符串夹在中间的换行,要用下面的正则式,出自这里的回答:
PHP也有trim()函数可用,一样是去除空白、tab与字符串的前后换行,例子如下:
去除字符串的夹在中间的换行也是用正则式,出自这里的回答:
https://github.com/pandao/edi...
http://markitup.jaysalvat.com...
http://www.codingdrama.com/bo...
js正则"xxx".replace(/(^s)|(s$)/g, "");