php/正则表达式:“链接化”博客标题

发布于 2024-09-10 04:09:07 字数 308 浏览 7 评论 0原文

我正在尝试编写一个简单的 PHP 函数,它可以接受像

Topic: Some stuff, Maybe some more, it's my stuff? 这样的

字符串,并返回

topic-some-stuff-maybe-some -more-its-my-stuff

因此:

  • 小写
  • 删除所有非字母数字非空格字符
  • 用连字符替换所有空格(或空格组)

我可以使用单个正则表达式来执行此操作吗?

I'm trying to write a simple PHP function that can take a string like

Topic: Some stuff, Maybe some more, it's my stuff?

and return

topic-some-stuff-maybe-some-more-its-my-stuff

As such:

  • lowercase
  • remove all non-alphanumeric non-space characters
  • replace all spaces (or groups of spaces) with hyphens

Can I do this with a single regex?

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

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

发布评论

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

评论(4

2024-09-17 04:09:07
function Slug($string)
{
    return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}

$topic = 'Iñtërnâtiônàlizætiøn';
echo Slug($topic); // internationalizaetion

$topic = 'Topic: Some stuff, Maybe some more, it\'s my stuff?';
echo Slug($topic); // topic-some-stuff-maybe-some-more-it-s-my-stuff

$topic = 'here عربي‎ Arabi';
echo Slug($topic); // here-arabi

$topic = 'here 日本語 Japanese';
echo Slug($topic); // here-japanese
function Slug($string)
{
    return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}

$topic = 'Iñtërnâtiônàlizætiøn';
echo Slug($topic); // internationalizaetion

$topic = 'Topic: Some stuff, Maybe some more, it\'s my stuff?';
echo Slug($topic); // topic-some-stuff-maybe-some-more-it-s-my-stuff

$topic = 'here عربي‎ Arabi';
echo Slug($topic); // here-arabi

$topic = 'here 日本語 Japanese';
echo Slug($topic); // here-japanese
淡莣 2024-09-17 04:09:07

您可以使用一个 preg_replace 来完成此操作:

preg_replace(array("/[A-Z]/e", "/\\p{P}/", "/\\s+/"),
    array('strtolower("$0")', '', '-'), $str);

从技术上讲,您可以使用一个正则表达式来完成此操作,但这更简单。

抢先响应:是的,它不必要地使用正则表达式(尽管非常简单),不必要地大量调用 strtolower,并且它不考虑非英语字符(他甚至不给出编码);我只是满足OP的要求。

You can do it with one preg_replace:

preg_replace(array("/[A-Z]/e", "/\\p{P}/", "/\\s+/"),
    array('strtolower("$0")', '', '-'), $str);

Technically, you could do it with one regex, but this is simpler.

Preemptive response: yes, it unnecessarily uses regular expressions (though very simple ones), an unecessarily big number of calls to strtolower, and it doesn't consider non-english characters (he doesn't even give an encoding); I'm just satisfying the OP's requirements.

甜是你 2024-09-17 04:09:07

为什么正则表达式被认为是解决所有生活问题的万能灵丹妙药(仅仅因为 preg_match 中的低级回溯发现了癌症的治疗方法)。这是一个不求助于正则表达式的解决方案:

$str = "Topic: Some stuff, Maybe some more, it's my stuff?";
$str = implode('-',str_word_count(strtolower($str),2));
echo $str;

不走整个 UTF-8 路线:

$str = "Topic: Some stuff, Maybe some more, it's my Iñtërnâtiônàlizætiøn stuff?";
$str = implode('-',str_word_count(strtolower(str_replace("'","",$str)),2,'Þßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'));
echo $str;

给出

主题-一些-东西-也许-更多-它-我的-iñtërnâtiônàlizætiøn-东西

Why are regular expressions considered the universal panacea to all life's problems (just because a lowly backtrace in a preg_match has discovered the cure for cancer). here's a solution without recourse to regexp:

$str = "Topic: Some stuff, Maybe some more, it's my stuff?";
$str = implode('-',str_word_count(strtolower($str),2));
echo $str;

Without going the whole UTF-8 route:

$str = "Topic: Some stuff, Maybe some more, it's my Iñtërnâtiônàlizætiøn stuff?";
$str = implode('-',str_word_count(strtolower(str_replace("'","",$str)),2,'Þßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'));
echo $str;

gives

topic-some-stuff-maybe-some-more-its-my-iñtërnâtiônàlizætiøn-stuff

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