preg_replace - 留下不需要的字符

发布于 2024-07-29 05:15:50 字数 822 浏览 5 评论 0原文

我有一个字符串:

$string = "Hello World!";

我想将其转换为 URL 友好标记,并且我开发了一个函数来执行此操作:

function stripJunk($string){
    $string = str_replace(" ", "-", $string);
    $string = preg_replace("/[^a-zA-Z]\s/", "", $string);
    $string = strtolower($string);
    return $string;
}

但是,当我在上面运行我的 $string 时,我得到以下内容:

$string = "hello-world!";

似乎有一些字符从我的 preg_replace 中溜走,即使根据我的理解,它们不应该如此。

它应该是这样的:

$string = "hello-world";

这是怎么回事? (这应该很简单!)

编辑 1:我不知道正则表达式是初学者的东西,但无论如何。 此外,删除字符串中的 \s 不会产生所需的结果。

期望的结果是:

  1. 所有空格都转换为破折号。
  2. 所有剩余的非 AZ 或 0-9 字符都将被删除。
  3. 然后将该字符串转换为小写。

编辑 2+:稍微清理了我的代码。

I've got a string:

$string = "Hello World!";

I want to turn it into a URL friendly tag, and I've developed a function to do it:

function stripJunk($string){
    $string = str_replace(" ", "-", $string);
    $string = preg_replace("/[^a-zA-Z]\s/", "", $string);
    $string = strtolower($string);
    return $string;
}

However, when I run my $string through it above, I get the following:

$string = "hello-world!";

It seems that there are characters slipping through my preg_replace, even though from what I understand, they shouldn't be.

It should read like this:

$string = "hello-world";

What's going on here? (This should be easy peasy lemon squeasy!)

Edit 1: I wasn't aware that regular expressions were beginners stuff, but whatever. Additionally, removing the \s in my string does not produce the desired result.

The desired result is:

  1. All spaces are converted to dashes.
  2. All remaining characters that are not A-Z or 0-9 are removed.
  3. The string is then converted to lower case.

Edit 2+: Cleaned up my code just a little.

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

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

发布评论

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

评论(4

提笔书几行 2024-08-05 05:15:50

以下对我来说效果很好:

function stripJunk($string){
    $string = str_replace(" ", "-", trim($string));
    $string = preg_replace("/[^a-zA-Z0-9-]/", "", $string);
    $string = strtolower($string);
    return $string;
}

The following works just fine to me:

function stripJunk($string){
    $string = str_replace(" ", "-", trim($string));
    $string = preg_replace("/[^a-zA-Z0-9-]/", "", $string);
    $string = strtolower($string);
    return $string;
}
撩动你心 2024-08-05 05:15:50

模式末尾的 \s 意味着您将仅替换紧随其后的空白字符的非字母字符。 您可能希望将 \s 放在方括号内,以便也保留空格,并且稍后可以用破折号替换。

如果您还想允许使用数字,则需要在方括号内添加 0-9。

例如:

<?php

$string = "Hello World!";

function stripJunk($string){
    $string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
    $string = str_replace(" ", "-", $string);
    $string = strtolower($string);
    return $string;
}

echo stripJunk($string);

The \s at the end of your pattern means that you will only replace non-alphabetical characters which are immediately followed by a whitespace character. You probably want the \s within the square brackets so that whitespace is also preserved and can later be replaced with a dash.

You will need to add 0-9 inside the square brackets if you want to also allow numbers.

For example:

<?php

$string = "Hello World!";

function stripJunk($string){
    $string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
    $string = str_replace(" ", "-", $string);
    $string = strtolower($string);
    return $string;
}

echo stripJunk($string);
扶醉桌前 2024-08-05 05:15:50

您可以连续使用一些正则表达式来删除垃圾:

<?php

function strip_junk ($string) {

  // first, strip whitespace; and replace every non-alphabetic character by a dash
  $string = preg_replace("/[^a-z0-9-]/u", "-", strtolower(trim($string)));

  // second, remove double dashes
  $string = preg_replace("/-+/u", "-", $string);

  // finally, remove leading and trailing dashes
  $string = preg_replace("/^-*|-*$/u", "", $string);

  return $string;

}

?>

这应该可以解决问题,快乐的 PHP'ing!

You could use some regular expressions in a row to remove the junk:

<?php

function strip_junk ($string) {

  // first, strip whitespace; and replace every non-alphabetic character by a dash
  $string = preg_replace("/[^a-z0-9-]/u", "-", strtolower(trim($string)));

  // second, remove double dashes
  $string = preg_replace("/-+/u", "-", $string);

  // finally, remove leading and trailing dashes
  $string = preg_replace("/^-*|-*$/u", "", $string);

  return $string;

}

?>

This should do the trick, happy PHP'ing!

记忆で 2024-08-05 05:15:50

那这个呢?

preg_replace("/[.\n\r][^a-zA-Z]/", "", $string);

如果这不起作用:

preg_replace("/[.\n\r^a-zA-Z]/", "", $string);

那有效吗?

What about this?

preg_replace("/[.\n\r][^a-zA-Z]/", "", $string);

if that does not work:

preg_replace("/[.\n\r^a-zA-Z]/", "", $string);

Does that work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文