问题
I'm using CKeditor for a text editor and it won't remove extra whitespace.
I've tried
$foo = strip_tags($foo);
$foo = preg_replace('/\s+/',' ',$foo);
I don't know of any other way to remove whitespace from here. Any ideas?
Thanks
回答1:
try these:
$foo = preg_replace('/\s{2,}/', ' ', $foo);
or
$foo = preg_replace('/( )+/', ' ', $foo);
or this one removes line breaks too
$foo = trim(preg_replace('/[\s\t\n\r\s]+/', ' ', $foo))
Update
Try this one:
$foo = trim(preg_replace('/( )+|\s\K\s+/','',$foo));
回答2:
Solved If anybody is curious, I solved this by cleaning them before they went into the database.
$cpbody = trim($_POST['cbody']);
$cpbody = preg_replace("/\<p\>\ \;\<\/p\>/", "", $cpbody);
$cpbody = preg_replace("/\ \;+/", " ", $cpbody);
$cpbody = preg_replace("/\s+/", " ", $cpbody);
$cpbody = htmlentities($cpbody);
来源:https://stackoverflow.com/questions/24579078/ckeditor-remove-extra-whitespace