无法获取str_replace()来删除PHP字符串中的空格 [英] Can't get str_replace() to strip out spaces in a PHP string

查看:94
本文介绍了无法获取str_replace()来删除PHP字符串中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个PHP字符串,需要将空格删除.我使用了以下代码,但是当我回显$classname时,它仅显示字符串,其中仍带有空格.

Hi, I am getting a PHP string which I need to strip the spaces out of. I have used the following code but when I echo $classname it just displays the string still with the spaces in it.

   <?php
     $fieldname = the_sub_field('venue_title');
     $classname = str_replace(' ', '', $fieldname);
     echo $classname;
   ?>

推荐答案

尝试为正则表达式模式添加 u 参数,因为字符串可以具有UTF-8编码:

Try to add u-parameter for regex-pattern, because a string can have UTF-8 encoding:

$classname  =  preg_replace('/\s+/u', '', $fieldname);

这篇关于无法获取str_replace()来删除PHP字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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