简体   繁体   English

如何用连字符替换点,空格和逗号,并使用php避免双连字符?

[英]How to replace dots, spaces and commas with hyphen and avoid double hyphen using php?

When I replace spaces, dots and commas of a string, it sometimes happens that I get double hyphens. 当我替换字符串的空格,点和逗号时,有时会出现双连字符。

For example turns check out the 1. place into check-out-the-1--place 例如check out the 1. place check-out-the-1--place check out the 1. place转换check-out-the-1--place

How can I avoid that? 我该如何避免呢? I want it to be check-out-the-1-place - so that there only is one hyphen between each word. 我希望它可以check-out-the-1-place -以便每个单词之间只有一个连字符。 Here is my code: 这是我的代码:

str_replace([' ', ',', '.','?'], '-', strtolower($pathname));

Right now, I know why it returns the double-hyphens, but I don't know how to work around that. 现在,我知道为什么它会返回双连字符,但是我不知道如何解决。

Can someone help me out? 有人可以帮我吗?

How can I avoid that? 我该如何避免呢? I want it to be check-out-the-1-place - so that there only is one hyphen between each word. 我希望它可以在1位签出-以便每个单词之间只有一个连字符。 Here is my code: 这是我的代码:

Whilst Mohammad's answer is nearly there, here is a more fully working PCRE regex method and explanation as to how it works, so you can use it as you need: 尽管Mohammad的答案近在咫尺,但这里有一个更完善的PCRE regex方法以及如何工作的解释,因此您可以根据需要使用它:

$str = trim(strtolower($pathname));
$newStr = preg_replace('/[\s.,-]+/', '-', $str);

How this works: 工作原理:

  • Match a single character present in the list below [\\s.,-]+ 匹配[\\s.,-]+下面列表中的单个字符
    • + Quantifier Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) +量词在一次和无限次之间进行匹配, 尽可能多次匹配,并根据需要返回(贪婪)
    • \\s matches any whitespace character (equal to [ \\r\\n\\t\\f\\v ]) \\s匹配任何空格字符(等于[ \\r\\n\\t\\f\\v ])
    • .,- matches a single character in the list .,- (case sensitive) .,-匹配列表中的单个字符.,- (区分大小写)
    • The dash - must come at the end of the [] set. 破折号-必须位于[]集的末尾

Results: 结果:

This: check out the 1. place 这:签出1.地方

Becomes: 成为:

check-out-the-1-place

And

This: check out the - 1. place 这:签出-1.地点

Becomes 成为

check-out-the-1-place


Further: 进一步:

I would go further and assuming you are using this for a URL slug ( a what?! ); 我会走得更远,并假设您正在使用它来处理URL子弹( 什么?! ); strip out all non-alphanumeric characters from the string and replace with a single - as per typical website slugs. 从字符串中去掉所有非字母数字字符,并用一个单一的取代-按典型网站蛞蝓。

 $newStr = preg_replace('/[^a-z0-9]+/i', '-', $str);

How this works: 工作原理:

  • Match a single character NOT ( ^ ) present in the list below [a-z0-9]+ 匹配[a-z0-9]+下面列表中的单个字符NOT^
    • + Quantifier Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) +量词在一次和无限次之间进行匹配, 尽可能多次匹配,并根据需要返回(贪婪)
    • az a single character in the range between a (index 97) and z (index 122) (case sensitive) az在(索引97)和z(索引122)之间的范围内的单个字符(区分大小写)
    • 0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive) 0-9单个字符,介于0(索引48)和9(索引57)(区分大小写)之间
    • The i at the end indicates the judgements are case In-sensitive . 最后的i表示判断不区分大小写

Example: 例:

check out - the no.! 退房-号! 1. Place 1.地点

Becomes: 成为:

check-out-the-1-Place

You can use preg_replace() instead and user regex to selecting multiple specific character. 您可以改用preg_replace()和用户正则表达式来选择多个特定字符。

$newStr = preg_replace("/[\s.,]+/", "-", $str)

Check result in demo 演示中检查结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM