PHP str_replace用下划线替换空格 [英] PHP str_replace replace spaces with underscores

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

问题描述

有没有我看不到的原因,为什么这不起作用?

Is there a reason that I'm not seeing, why this doesn't work?

    $string = $someLongUserGeneratedString;
    $replaced = str_replace(' ', '_', $string);
    echo $replaced;

输出中仍然包含空格...任何想法都很棒

The output still includes spaces... Any ideas would be awesome

推荐答案

我建议您使用它,因为它将检查一次或多次出现的空白(如Lucas Green所建议的那样).

I'll suggest that you use this as it will check for both single and multiple occurrence of white space (as suggested by Lucas Green).

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

代替:

$journalName = str_replace(' ', '_', $journalName);

这篇关于PHP str_replace用下划线替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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