代码之家  ›  专栏  ›  技术社区  ›  ganjan

preg_replace“.php”?

  •  1
  • ganjan  · 技术社区  · 14 年前

    如何从字符串末尾preg_replace.php?

    for ($x = 0; $x < count($sidebar_links); $x++) {
    
       $sidebar[$x] = $x;           
    }
    

    $BasBar链接:

    array(3) { [0]=>  string(9) "index.php" [1]=>  string(9) "site2.php" [2]=>  string(7) "site3.php" } 
    
    3 回复  |  直到 14 年前
        1
  •  4
  •   Haim Evgi    14 年前

    你可以用简单的 str_replace

    喜欢

    str_replace('.php', '', $sidebar_links[$x]);
    

    basename

    basename($sidebar_links[$x], ".php");
    
        2
  •  3
  •   Gabi Purcaru BornCoder    14 年前

    preg_replace('\.php$', '', $sidebar_link)

    或者,为了避免regex:

    if (substr($sidebar_link, -4) == ".php")
         $sidebar_link = substr($sidebar_link, 0, -4);
    
        3
  •  0
  •   Ayaz Alavi    14 年前

    我使用以下代码替换文件扩展名。而不是替换,只是取消设置数组的最后一个索引。

    private function changeFileExtension($imagePath, $ext=""){
            $arr = explode(".", $imagePath);
            if(count($arr) > 0)
            {
                $arr[count($arr)-1] = $ext;
                return implode(".", $arr);
            }
            else
                return false;
        }