带自定义分隔符的ucwords函数返回null

时间:2017-10-12 11:08:18

标签: php

我尝试将字符串转换为CamelCase,然后再将其插入数据库。我正在使用ucwords。我的字符串就像FIRST_SECOND_THIRD

    if (//something){
    $resp = strtolower($line[14]);
    $resp_ = ucwords($resp, "_");

    //rest of the query...

    }

var_dump($resp_)返回null,我不知道为什么...... 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

这确实是一样的事情,希望能为您加油。

// php脚本

<?php

    $string = "FIRST_SECOND_THIRD";

    $var = strtolower(str_replace('_',' ',$string)); 
    $temp = ucwords($var);
    echo str_replace(' ', '', $temp);

?>

//output 
FirstSecondThird

如果自定义分隔符适用于ucwords函数,则工作可能会容易一些。

答案 1 :(得分:0)

如果您输入的字符串完全是大写字母,那么您的目的是在字符串的开头或下划线后的跟随字母的字母上使用<ul class="nav ml-auto justify-content-end" id="vpMenu">

代码:(Demo

strtolower()

输出:

echo preg_replace_callback('~(?:^|_)[A-Z]\K[A-Z]+~', function($m){ return strtolower($m[0]);}, 'FIRST_SECOND_THIRD');
相关问题