PHP-字符串操作删除特殊字符并替换空格 [英] PHP - String manipulation remove spcial characters and replace spaces

查看:196
本文介绍了PHP-字符串操作删除特殊字符并替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中获取字符串,然后使用这些字符串来构建URL.我的问题是,某些字符串将包含<之类的字符. >& {} *一般特殊字符,但字符串中也可能包含字符串.我如何用破折号替换空格,并从字符串中完全删除特殊字符?

I am getting strings from a database and then using the strings to build a URL. My issue is, some of the strings will have characters like < > & { } * general special characters, but the string may also have strings in. How would I replace the spaces with dashes and totally remove and special characters from the strings?

推荐答案

使用 str_replace :

$str = str_replace(array(' ', '<', '>', '&', '{', '}', '*'), array('-'), $str);

注意:

如果 replace 的值少于 search ,则将空字符串用于其余替换值.

If replace has fewer values than search, then an empty string is used for the rest of replacement values.

这篇关于PHP-字符串操作删除特殊字符并替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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