Ruby 帖子标题到 slug

发布于 2024-10-05 19:49:04 字数 188 浏览 0 评论 0原文

我应该如何在 Ruby 中将帖子标题转换为 slug?

标题可以包含任何字符,但我只希望 slug 允许 [a-z0-9-_] (它应该允许任何其他字符吗?)。

所以基本上:

  • 所有字母都小写
  • 将空格转换为连字符
  • 删除无关的字符

How should I convert a post title to a slug in Ruby?

The title can have any characters, but I only want the slug to allow [a-z0-9-_] (Should it allow any other characters?).

So basically:

  • downcase all letters
  • convert spaces to hyphens
  • delete extraneous characters

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

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

发布评论

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

评论(5

堇色安年 2024-10-12 19:49:04

这是铁轨吗? (在 Sinatra 中工作)

string.parameterize

就是这样。对于更复杂的重击,请参阅 ActsAsUrl。它可以执行以下操作:

"rock & roll".to_url => "rock-and-roll"
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"
"kick it en Français".to_url => "kick-it-en-francais"
"rock it Español style".to_url => "rock-it-espanol-style"
"tell your readers 你好".to_url => "tell-your-readers-ni-hao"

Is this Rails? (works in Sinatra)

string.parameterize

That's it. For even more sophisticated slugging, see ActsAsUrl. It can do the following:

"rock & roll".to_url => "rock-and-roll"
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"
"kick it en Français".to_url => "kick-it-en-francais"
"rock it Español style".to_url => "rock-it-espanol-style"
"tell your readers 你好".to_url => "tell-your-readers-ni-hao"
慢慢从新开始 2024-10-12 19:49:04
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')

downcase 使其变为小写。 strip 确保没有前导或尾随空格。第一个 gsub 用连字符替换空格。第二个 gsub 删除所有非字母非破折号非下划线字符(请注意,该集非常接近 \W 但也包含破折号,这就是为什么此处已详细说明)。

slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')

downcase makes it lowercase. The strip makes sure there is no leading or trailing whitespace. The first gsub replaces spaces with hyphens. The second gsub removes all non-alpha non-dash non-underscore characters (note that this set is very close to \W but includes the dash as well, which is why it's spelled out here).

仙女山的月亮 2024-10-12 19:49:04

to_slug 是一个很棒的 Rails 插件,可以处理几乎所有内容,包括时髦的字符,但它的实现非常简单。把它扔到字符串上,你就会被排序。这是浓缩的来源:

String.class_eval do
  def to_slug
    value = self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s
    value.gsub!(/[']+/, '')
    value.gsub!(/\W+/, ' ')
    value.strip!
    value.downcase!
    value.gsub!(' ', '-')
    value
  end
end

to_slug is a great Rails plugin that handles pretty much everything, including funky characters, but its implementation is very simple. Chuck it onto String and you'll be sorted. Here's the source condensed down:

String.class_eval do
  def to_slug
    value = self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s
    value.gsub!(/[']+/, '')
    value.gsub!(/\W+/, ' ')
    value.strip!
    value.downcase!
    value.gsub!(' ', '-')
    value
  end
end
清风夜微凉 2024-10-12 19:49:04

我用过这个 gem。它很简单但很有用。

https://rubygems.org/gems/string_helpers

I've used this gem.It's simple but helpful.

https://rubygems.org/gems/string_helpers

淡水深流 2024-10-12 19:49:04

我喜欢FriendlyId,它自称是创造鼻涕虫的“瑞士陆军推土机”。 https://github.com/norman/friend_id

I like FriendlyId, the self-professed "Swiss Army Bulldozer" of creating slugs. https://github.com/norman/friendly_id

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