繁体   English   中英

如何用PHP中给定字符串中的空格替换字符?

[英]How to replace a character with white space from a given string in php?

$country = preg_replace(array('/^\[/','/\]$/'), '',$country); 

这样将根据此问题在文本中的任何位置删除方括号

但是可以说我们有:

United_arab_emirates

我们如何才能做到这一点:

United Arab Emirates

注意:此处不是php专家*

这就是我尝试过的(由于这里的答案之一)

$custom_country = usp_get_meta(false, 'usp-custom-3');
$custom_country = htmlspecialchars_decode($custom_country);
$custom_country = nl2br($custom_country);
$country = preg_replace(array('_'), ' ',$country); 
echo ucfirst($custom_country);

结果仍然是United_arab_emirates

最快的方法是两个步骤:

  1. 用空格替换下划线
  2. 每个单词的大写首字母

码:

$country = str_replace('_', ' ', $country);
$country = ucwords($country);
$country = preg_replace(array('[_]'), ' ',$country); 

或者你可以只使用字符串替换

$country = str_replace("_"," ",$country);

如果_是唯一需要替换的额外字符,则可以使用str_replace

$country = str_replace("_", " ", $country); 

详细信息: http : //php.net/manual/zh/function.str-replace.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM