PHP - 字符串操作删除特殊字符并替换空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4005282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:42:58  来源:igfitidea点击:

PHP - String manipulation remove spcial characters and replace spaces

phpstring

提问by sea_1987

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?

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

采纳答案by Felix Kling

With str_replace:

str_replace

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

Note:

笔记:

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

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

回答by Naveed

Keep only alphabets and numbers in a string using preg_replace:

使用preg_replace以下方法仅保留字符串中的字母和数字:

$string = preg_replace('/[^a-zA-Z0-9-]/', '', $string);

You can use str_replaceto replace space with -

您可以使用str_replace- 替换空间

$string = str_replace (" ", "-", $string);

Look at the following article:

看看下面的文章:

回答by Vincent Savard

1) Replace diacritics with iconv
2) Replace non letter characters with empty string
3) Replace spaces with dash
4) Trim the string for the dash characters (you can also trim the string before manipulations)

1) 用 iconv 替换变音符号
2) 用空字符串替换非字母字符
3) 用破折号替换空格
4) 修剪破折号字符的字符串(您也可以在操作前修剪字符串)

Example, if you use UTF-8 encoding :

例如,如果您使用 UTF-8 编码:

setlocale(LC_ALL, 'fr_CA.utf8');
$str = "%#d?dèàa.,d s#.sèdf;21df";

$str = iconv("UTF-8", "ASCII//TRANSLIT", $str); // "%#dsdeaa.,d s#.sedf;21df"
$str = preg_replace("`[^\w]+`", "", $str); // "dsdeaad s4sedf21df"
$str = str_replace(" ", "-", $str); // "dsdeaad-s4sedf21df"
$str = trim($str, '-'); // "dsdeaad-s4sedf21df"

回答by dpkrai96

$search_value =array(",",".",'"',"'","\"," ","/","&","[","]","(",")"," 
{","}",":","`","!","@","#","%","=","+");
$replace_value =array("-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","- 
","-","-","-","-","-");
$product_name = str_replace($search_value,$replace_value,$row["Product_Name"]);
$product_name = str_replace("--","-",$product_name);
$product_name = str_replace("--","-",$product_name);
$product_name = preg_replace('/-$/', '', $product_name);
$product_name = preg_replace('/^-/', '', $product_name);

This will create dashed string (Only have alphanumeric character with dash).Useful for create URI strings.

这将创建破折号字符串(只有带破折号的字母数字字符)。用于创建 URI 字符串。

回答by Orbit

str_replace(' ','-',$string);

alphanumeric:

字母数字:

$output = preg_replace("/[^A-Za-z0-9]/","",$input); 

if you want to keep the characters:

如果你想保留字符:

 htmlspecialchars($string);