有谁知道如何在PHP中爆炸一些字符串,例如:
<?
// Submitted through register form
$first_name = $_POST['first_name'];
// example $_POST['first_name'] is somebody
// and need to be exploded at position 1 or by letter 'o' in this example
// but that is not always letter o
// I made also
$first_name_lc = strtolower($first_name);
// converted all to lowers and now
// exploding at letter o
$explode_rule = 'o';
$first_name_lc_exploded = explode($explode_rule, $first_name_lc);
// now in example we have $first_name_lc_exploded[0] and $first_name_lc_exploded[1]
// $first_name_lc_exploded[0] is now 's' and $first_name_lc_exploded[1] is 'mebody'
// now new first name is $first_name_lc_exploded[0].$explode_rule.$first_name_lc_exploded[1]
// but $first_name_lc_exploded[0] need to be converted in upper and only that, nothing else
// than $first_name_lc_exploded[0] is 'S' instead of 's'
// and first_name is now 'Somebody' instead of 'somebody'
?>
因此,现在的问题是:如何用寻找字符串内第二个字母的东西替换字母o,并使用该规则(某个变量)将其分解。有人有主意吗?
最佳答案
如果您只是想大写第一个字母,请尝试ucfirst(strtolower($first_name));
如果要大写空格后出现的所有字母,请尝试ucwords(strtolower($first_name));
;
如果您确实想在第二个字母上分割字符串,请使用substr();
提取想要的字符串部分。
另一种选择是使用preg_split();
并使用正则表达式指示字符串的形状以及您希望如何拆分。
或者,您始终可以explode($first_name[1], $first_name, 2));
http://php.net/manual/en/function.ucfirst.php
http://php.net/manual/en/function.ucwords.php
http://php.net/manual/en/function.substr.php
http://php.net/manual/en/function.preg-split.php
关于php - PHP中爆炸的字符串,用于将一个部分转换为大写字母,而另一部分转换为小写字母?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16106250/