如何将所有非单词字符和多个空格更改为 ' '然后所有空间都为“-”在一个 preg_replace() 中

发布于 2024-09-25 00:55:53 字数 531 浏览 4 评论 0原文


我想在以下条件下更改图像名称

  • 所有非单词字符都会变成空格,
  • 然后所有空格都会变成 -

表示如果我的图像名称是: Hello My name is'Kh@n "Mr .Khan " 则应更改为 Hello-My-name-is-kh-n-mr-Khan

我需要分两步使用下面的内容,

$old_name ='       Hello       My name is\'Kh@n "Mr. Khan        ';
$space_name = preg_replace('/\W/',' ', $old_name);
$new_name= preg_replace('/\s+/','-', $space_name);

echo $new_name // returns Hello-My-name-is-kh-n-mr-Khan

有什么方法可以在一步中应用这两个条件吗?

I want to change image names on below conditions

  • All non word characters turns into space and
  • then all spaces turns into -

means if my image name is : Hello My name is'Kh@n "Mr. .Khan " then it should be changed into Hello-My-name-is-kh-n-mr-Khan .

i need to use below in two steps,

$old_name ='       Hello       My name is\'Kh@n "Mr. Khan        ';
$space_name = preg_replace('/\W/',' ', $old_name);
$new_name= preg_replace('/\s+/','-', $space_name);

echo $new_name // returns Hello-My-name-is-kh-n-mr-Khan

is there any way to apply both conditions in single step??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

数理化全能战士 2024-10-02 00:55:53

preg_replace 可以采用数组:

$new_name = preg_replace(array('/\s+/', '/\W/'), array('_', '-'), $old_name);

这更简洁,但我不相信它有任何意义更有效率。该文档没有指定应用正则表达式的顺序。由于空格字符是非单词字符,因此更安全的版本是:

$new_name = preg_replace(array('/\s+/', '/[^\s\w]/'), array('_', '-'), $old_name);

preg_replace can take arrays:

$new_name = preg_replace(array('/\s+/', '/\W/'), array('_', '-'), $old_name);

This is more succinct, but I don't believe it's any more efficient. The docs don't specify an order that the regexps are applied. Since space characters are non-word characters, a safer version is:

$new_name = preg_replace(array('/\s+/', '/[^\s\w]/'), array('_', '-'), $old_name);
软的没边 2024-10-02 00:55:53

我发现这个函数的输出(Hello_My_name_is-Kh-n_-Mr-_Khan_)有点难看
这是我的方法

$name ='Hello       My name is\'Kh@n "Mr. Khan" ';
$name = preg_replace('/\W/',' ', $name);
$name = trim($name);
$name = strtolower($name);
$name = preg_replace('/\s+/','-', $name);

输出
你好-我的名字-is-kh-n-mr-khan

I find this function's output (Hello_My_name_is-Kh-n_-Mr-_Khan_)a bit ugly
Here is my approach

$name ='Hello       My name is\'Kh@n "Mr. Khan" ';
$name = preg_replace('/\W/',' ', $name);
$name = trim($name);
$name = strtolower($name);
$name = preg_replace('/\s+/','-', $name);

outputs
hello-my-name-is-kh-n-mr-khan

抽个烟儿 2024-10-02 00:55:53

您可以在 preg_replace 中使用数组:

$old_name ='Hello       My name is\'Kh@n "Mr. Khan ';
$new_name = preg_replace(array('/\s+/','/\W/'),array('_','-'),$old_name);

另外,

您的代码片段有语法错误,您需要在 K 之前转义单引号

You can use arrays in preg_replace:

$old_name ='Hello       My name is\'Kh@n "Mr. Khan ';
$new_name = preg_replace(array('/\s+/','/\W/'),array('_','-'),$old_name);

Also,

Your snippet has syntax error, you need to escape the single quote before K

半边脸i 2024-10-02 00:55:53

这是要重命名上传的图片吗?如果是这样,请记住...

  • 最大字符数(应该将其剪裁为合理的)
  • 非 ASCII 字符? (最好是音译)

Is this to rename uploaded images? If so, keep in mind...

  • max char count (should snip it at something sensible)
  • non ASCII chars? (transliterate would be best)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文