删除php 中生成的空格

发布于 2024-11-27 22:22:49 字数 1181 浏览 1 评论 0原文

我遇到的问题是删除空格。 我尝试了几种不同的方法,但都没有将它们删除。 我有一个下拉菜单,其中有空格,以便所有时间都排列起来以便客户轻松理解。它就像下面的 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栩栩如生 2024-12-04 22:22:49

不间断空格不是普通空格。

str_replace("\xa0",'',$string);

或者获取所有其他空白:

preg_replace("/[\xa0\s]{2,}/",'',$string);

并注意字符编码,您似乎对此有一些问题。

A non-breaking space is not a normal space.

str_replace("\xa0",'',$string);

Or to get all other whitespace in there:

preg_replace("/[\xa0\s]{2,}/",'',$string);

And be aware of character encoding, you seem to have some issues with that.

别低头,皇冠会掉 2024-12-04 22:22:49

使用这个:

$string = preg_replace('/\s+/', '', $string);

Use this :

$string = preg_replace('/\s+/', '', $string);
别在捏我脸啦 2024-12-04 22:22:49

好的,多一点代码,但可以工作

$str = "  9:00 AM  to  11:00 AM";  

$find_cln = array (' ','to','AM','PM'); // find
$rep_cln = array ('',' to ',' AM',' PM'); // replace

$str = str_replace($find_cln, $rep_cln, $str);

okay bit more code but works

$str = "  9:00 AM  to  11:00 AM";  

$find_cln = array (' ','to','AM','PM'); // find
$rep_cln = array ('',' to ',' AM',' PM'); // replace

$str = str_replace($find_cln, $rep_cln, $str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文