删除php变量,用破折号替换空白 [英] Strip php variable, replace white spaces with dashes

查看:76
本文介绍了删除php变量,用破折号替换空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将PHP变量从我的公司和我的名字"转换为我的公司-我的名字"?

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 replace spaces with dashes.

推荐答案

此函数将创建SEO友好字符串

This function will create an SEO friendly string

function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}

应该没问题:)

这篇关于删除php变量,用破折号替换空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆