Php 在字符串的一部分将snake_盒替换为CamelCase盒

Php 在字符串的一部分将snake_盒替换为CamelCase盒,php,Php,我有一个uri,看起来像: /Albums/album_id/Photos/photo_id/tags 我想用AlbumId和PhotoId替换AlbumId和PhotoId。我如何做到这一点?2个步骤: a_u之后的每个字母都应该大写 _应该删除 实施: 让我们将字符串设为字符数组: $a = "/Albums/album_id/Photos/photo_id/tags" $b = str_split($a) 现在,\uuu后面的每个字符都应该大写: for ($i = 1; $i <

我有一个uri,看起来像:

/Albums/album_id/Photos/photo_id/tags

我想用
AlbumId
PhotoId
替换
AlbumId
PhotoId
。我如何做到这一点?

2个步骤:

  • a_u之后的每个字母都应该大写
  • _应该删除
  • 实施:

    让我们将字符串设为字符数组:

    $a = "/Albums/album_id/Photos/photo_id/tags"
    $b = str_split($a)
    
    现在,
    \uuu
    后面的每个字符都应该大写:

    for ($i = 1; $i < count($b); $i++) {
        if ($b[i - 1] == '_') {
            $b[i] = strtoupper($b[i]);
        }
    }
    
    并卸下
    \uu
    s

    e = str_replace("_", "", $c);
    
    和完整版本:

    function doThat($t) {
      $b = str_split($t)
      for ($i = 1; $i < count($b); $i++) {
        if ($b[i - 1] == '_') {
          $b[i] = strtoupper($b[i]);
        }
      }
      return str_replace("_", "", implode("", $b));
    }
    
    函数doThat($t){
    $b=str_分割($t)
    对于($i=1;$i
    两个步骤:

  • a_u之后的每个字母都应该大写
  • _应该删除
  • 实施:

    让我们将字符串设为字符数组:

    $a = "/Albums/album_id/Photos/photo_id/tags"
    $b = str_split($a)
    
    现在,
    \uuu
    后面的每个字符都应该大写:

    for ($i = 1; $i < count($b); $i++) {
        if ($b[i - 1] == '_') {
            $b[i] = strtoupper($b[i]);
        }
    }
    
    并卸下
    \uu
    s

    e = str_replace("_", "", $c);
    
    和完整版本:

    function doThat($t) {
      $b = str_split($t)
      for ($i = 1; $i < count($b); $i++) {
        if ($b[i - 1] == '_') {
          $b[i] = strtoupper($b[i]);
        }
      }
      return str_replace("_", "", implode("", $b));
    }
    
    函数doThat($t){
    $b=str_分割($t)
    对于($i=1;$i
    您可以检查此代码:

    $str = "/Albums/album_id/Photos/photo_id/tags";
    
    $split_str = split("_", $str);
    
    $new_str = $split_str[0];
    
    for ($i = 1; $i < count($split_str); $i++)
    {
        $new_str .= ucfirst($split_str[$i]);
    }
    
    echo $new_str;  
    
    $str=“/Albums/album\u id/Photos/photo\u id/tags”;
    $split_str=split(“_”,$str);
    $new_str=$split_str[0];
    对于($i=1;$i

    您可以检查此代码:

    $str = "/Albums/album_id/Photos/photo_id/tags";
    
    $split_str = split("_", $str);
    
    $new_str = $split_str[0];
    
    for ($i = 1; $i < count($split_str); $i++)
    {
        $new_str .= ucfirst($split_str[$i]);
    }
    
    echo $new_str;  
    
    $str=“/Albums/album\u id/Photos/photo\u id/tags”;
    $split_str=split(“_”,$str);
    $new_str=$split_str[0];
    对于($i=1;$i

    也许这有帮助:

    <?php
    function convert($string){
      $sections = explode('/',$string);
      $url = '';
      foreach ($sections as $section){
      $words = explode('_',$section);
        if(count($words) > 1){
          foreach ($words as $word){
            $word = ucfirst($word);
            $url .= $word;
          }
        } else {
          $url .= $words[0];
        }
      $url .= '/';
      }
      return $url;
    }
    $url = convert("/Albums/album_id/Photos/photo_id/tags");
    echo $url;
    ?>
    
    
    
    这使得单个小写单词保持不变,并且对于像ab_cd这样的部分,仅大写并删除破折号。现在末尾有一个斜杠,如果你愿意,当然可以简单地去掉它。

    也许这有帮助:

    <?php
    function convert($string){
      $sections = explode('/',$string);
      $url = '';
      foreach ($sections as $section){
      $words = explode('_',$section);
        if(count($words) > 1){
          foreach ($words as $word){
            $word = ucfirst($word);
            $url .= $word;
          }
        } else {
          $url .= $words[0];
        }
      $url .= '/';
      }
      return $url;
    }
    $url = convert("/Albums/album_id/Photos/photo_id/tags");
    echo $url;
    ?>
    
    
    

    这使得单个小写单词保持不变,并且对于像ab_cd这样的部分,仅大写并删除破折号。现在末尾有一个斜杠,如果您愿意,当然可以简单地去掉它。

    只需调用一次函数即可:

    $string = '/Albums/album_id/Photos/photo_id/tags';
    
    echo preg_replace_callback(
        '/_(.)/',
        function($match) {
            return strtoupper($match[1]);
        },
        $string);
    
    奇怪的笑脸实际上是regex,它匹配
    \uuu
    之后的任何字符。
    ()
    任何字符
    分为自己的组,以便我们可以在回调函数中的匹配数组中使用它

    在第一次正则表达式匹配时,
    $match
    成为一个数组,其中包含
    0=>\u i,1=>i
    ,因此您可以在回调函数中安全地使用
    $match[1]

    回调函数本身只返回转换为大写的字符串


    上述示例的输出是
    /Albums/albumId/Photos/photoId/tags

    只需调用一次函数即可:

    $string = '/Albums/album_id/Photos/photo_id/tags';
    
    echo preg_replace_callback(
        '/_(.)/',
        function($match) {
            return strtoupper($match[1]);
        },
        $string);
    
    奇怪的笑脸实际上是regex,它匹配
    \uuu
    之后的任何字符。
    ()
    任何字符
    分为自己的组,以便我们可以在回调函数中的匹配数组中使用它

    在第一次正则表达式匹配时,
    $match
    成为一个数组,其中包含
    0=>\u i,1=>i
    ,因此您可以在回调函数中安全地使用
    $match[1]

    回调函数本身只返回转换为大写的字符串


    上面示例的输出是
    /Albums/albumId/Photos/photoId/tags

    我最后使用了以下命令:

    str_replace(' ', '', ucwords(str_replace('_', ' ', str_replace('/', '/ ', $uri))));
    
    解释

    前缀
    /
    不允许
    foo\u-bar
    在使用
    ucwords(str\u-replace(“”,“”,$uri))
    时变为
    FooBar


    因此,首先将
    /
    替换为
    /
    (即在斜杠后添加一个空格),最后删除所有空格。

    我最后使用了以下命令:

    str_replace(' ', '', ucwords(str_replace('_', ' ', str_replace('/', '/ ', $uri))));
    
    解释

    前缀
    /
    不允许
    foo\u-bar
    在使用
    ucwords(str\u-replace(“”,“”,$uri))
    时变为
    FooBar


    因此,首先将
    /
    替换为
    /
    (即在斜杠后添加空格),最后删除所有空格。

    下面是另一行:

    echo implode('', array_map('ucfirst', explode('_', $string)));
    

    这是另一条单行线:

    echo implode('', array_map('ucfirst', explode('_', $string)));
    
    也可以尝试以下方法:

    $str = "/Albums/album_id/Photos/photo_id/tags";
    $str = str_replace("_", "", ucwords($str, " /_"));
    
    参考(它允许第二个参数,尽管有些php在线测试人员声明其他…)

    也可以尝试以下方法:

    $str = "/Albums/album_id/Photos/photo_id/tags";
    $str = str_replace("_", "", ucwords($str, " /_"));
    

    参考(它允许第二个参数,尽管有些php在线测试人员声明其他参数…)

    我测试了这个参数,它为该字符串和其他类似字符串提供了技巧: 它将/Albums/album\u id/Photos/photo\u id/tags转换为/Albums/album id/Photos/PhotoId/tags

    $str=“/Albums/album_id/Photos/photo_id/tags”

    $split\u str=explode(“/”,$str);
    $result='';
    对于($i=0;$i
    我测试了这个字符串,它对该字符串和其他类似字符串起到了作用: 它将/Albums/album\u id/Photos/photo\u id/tags转换为/Albums/album id/Photos/PhotoId/tags

    $str=“/Albums/album_id/Photos/photo_id/tags”

    $split\u str=ex