删除php 中生成的空格
我遇到的问题是删除空格。 我尝试了几种不同的方法,但都没有将它们删除。 我有一个下拉菜单,其中有空格,以便所有时间都排列起来以便客户轻松理解。它就像下面的 $str 一样。主要问题是,一旦发送到 mysql 数据库,它就会用带重音符号的 A 替换多余的空格。另一个问题是,最终看起来不正确,因为有额外的空间供客户在提交之前查看。我不清楚 mysql_prep 的功能,但在数据库上运行查询之前它也有修剪。
我尝试过:
$str = " 9:00 AM to 11:00 AM"
$time = str_replace(' ', '', $str);
echo $time; // OUTPUTS " 9:00AM to 11:00AM" // THOSE SPACES DONT NEED TO BE REMOVED
$str = " 9:00 AM to 11:00 AM"
$time = str_replace(' ', '', $str); //NOTE THE EXTRA SPACE
echo $time; // OUTPUTS " 9:00 AM to 11:00 AM"
$str = " 9:00 AM to 11:00 AM"
$time = str_replace(' ', '', $str); //EVEN TRIED THIS EVEN THOUGH THERES NO WAY
echo $time; // OUTPUTS " 9:00 AM to 11:00 AM"
我什
$str = " 9:00 AM to 11:00 AM"
$time = preg_replace('/\s+/', '', $str);
echo $time; // OUTPUTS "???9:00 AM??to??11:00 AM"//SEEMS CLOSEST BUT THE TRIANGLE QUESTION MARKS REPLACE THE SPACES
$str = " 9:00 AM to 11:00 AM"
$time = preg_replace('/\s\s+/', ' ', $str);
echo $time; // OUTPUTS " 9:00 AM to 11:00 AM"
至尝试使用explode和trim以一种迂回的方式来完成它,甚至trim也没有删除空格。我正在使用的版本; PHP:5.3.4,MySQL:5.1.53 和火狐浏览器:5.0.1
The problem I'm having is removing spaces.
I've tried a few different ways and none of them have removed them.
I have a drop down menu that has spaces in it so that all the times line up to be easily understood by a customer. It comes through like $str below. Main problem is that once its sent to the mysql database, it replaces the extra spaces with an A with an accent mark. The other problem is, it just doesn't look right in the end with the extra spaces for the customer to review before submitting. I'm not clear on the function of mysql_prep but its there as well with trim before the query is ran on the database.
I tried:
$str = " 9:00 AM to 11:00 AM"
$time = str_replace(' ', '', $str);
echo $time; // OUTPUTS " 9:00AM to 11:00AM" // THOSE SPACES DONT NEED TO BE REMOVED
$str = " 9:00 AM to 11:00 AM"
$time = str_replace(' ', '', $str); //NOTE THE EXTRA SPACE
echo $time; // OUTPUTS " 9:00 AM to 11:00 AM"
$str = " 9:00 AM to 11:00 AM"
$time = str_replace(' ', '', $str); //EVEN TRIED THIS EVEN THOUGH THERES NO WAY
echo $time; // OUTPUTS " 9:00 AM to 11:00 AM"
And
$str = " 9:00 AM to 11:00 AM"
$time = preg_replace('/\s+/', '', $str);
echo $time; // OUTPUTS "???9:00 AM??to??11:00 AM"//SEEMS CLOSEST BUT THE TRIANGLE QUESTION MARKS REPLACE THE SPACES
$str = " 9:00 AM to 11:00 AM"
$time = preg_replace('/\s\s+/', ' ', $str);
echo $time; // OUTPUTS " 9:00 AM to 11:00 AM"
I even tried using explode and trim to do it in a round about way and even trim didn't remove the spaces. Versions I'm using; PHP:5.3.4, MySQL:5.1.53 & Firefox:5.0.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不间断空格不是普通空格。
或者获取所有其他空白:
并注意字符编码,您似乎对此有一些问题。
A non-breaking space is not a normal space.
Or to get all other whitespace in there:
And be aware of character encoding, you seem to have some issues with that.
使用这个:
Use this :
好的,多一点代码,但可以工作
okay bit more code but works