Strip php variable, replace white spaces with dashes

前端 未结 3 2069
梦谈多话
梦谈多话 2020-12-04 07:21

How can I convert a PHP variable from \"My company & My Name\" to \"my-company-my-name\"?

I need to make it all lowercase, remove all special characters and repl

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 08:09

    Replacing specific characters: http://se.php.net/manual/en/function.str-replace.php

    Example:

    function replaceAll($text) { 
        $text = strtolower(htmlentities($text)); 
        $text = str_replace(get_html_translation_table(), "-", $text);
        $text = str_replace(" ", "-", $text);
        $text = preg_replace("/[-]+/i", "-", $text);
        return $text;
    }
    

提交回复
热议问题