PHP:str_replace() - 忽略空格 [英] PHP: str_replace() - ignore white space

查看:36
本文介绍了PHP:str_replace() - 忽略空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于 , ] }

我想用 ] 替换它 }

问题是我不知道,] 之间是否会有空格.可能有空白或可能没有空白.可能有制表符或换行符.

The problem is I don't know whether there will be white space between the , and the ]. There may be white space or there may be no white space. There may be tabs or there may be line breaks.

我怎样才能将 ,<此处的任何空格>] } 替换为 ] } 好吗?

How can I replace ,<any white space here>] } to just ] } please?

推荐答案

代替 str_replace 你可以简单地使用 preg_replace 就像

Instead of str_replace you can simply use preg_replace like as

echo preg_replace('/,\s*\]\s*}/',"] }",$string);

  • \s* \s 检查包含 new line,tabs,space* 用于零次或多次出现的空格
  • \]\s*} 这将检查指定的 ] brace \s*上面和 } 花括号字面意思
    • \s* \s checks for any space character that includes new line,tabs,space and * for zero or more occurence of space
    • \]\s*} this'll check for the ] brace \s* as specified above and } curly brace literally
    • 这篇关于PHP:str_replace() - 忽略空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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