我需要将 'jonathan-morón-16394669' 转换为 'jonathan-mor%C3%B3n-16394669'
Posted
技术标签:
【中文标题】我需要将 \'jonathan-morón-16394669\' 转换为 \'jonathan-mor%C3%B3n-16394669\'【英文标题】:I need to convert 'jonathan-morón-16394669' into 'jonathan-mor%C3%B3n-16394669'我需要将 'jonathan-morón-16394669' 转换为 'jonathan-mor%C3%B3n-16394669' 【发布时间】:2021-12-18 18:36:40 【问题描述】:我正在尝试解决一个问题,因为有时会保存诸如“jonathan-mor%C3%B3n-16394669”之类的数据,而有时会保存诸如“jonathan-morón-16394669”之类的数据。所以我需要一种平滑的方式在这两个版本的字符串之间移动,以便能够比较事物并通过匹配它们来实现它是相同的,并且继续进行而不在数据库中保存两次。
我知道我可以开始使用类似的东西,但反过来:
line = line.replace("%C3%A9","e")
line = line.replace("%C3%B3","o")
line = line.replace("%E2%80%93","-")
line = line.replace("%C3%A6","e")
line = line.replace("%C3%A8","e")
line = line.replace("%C3%A0","e")
line = line.replace("%C3%A1","i")
line = line.replace("%C5%82","l")
line = line.replace("%C5%84","n")
line = line.replace("%C3%BF", "y")
line = line.replace("%C3%BE", "p")
line = line.replace("%C3%BD", "y")
line = line.replace("%C3%BC", "u")
line = line.replace("%C3%BB", "u")
line = line.replace("%C3%BA", "u")
line = line.replace("%C3%B9", "o")
line = line.replace("%C3%B6", "o")
line = line.replace("%C3%B5", "o")
line = line.replace("%C3%B4", "o")
line = line.replace("%C3%B3", "o")
line = line.replace("%C3%B2", "o")
line = line.replace("%C3%B1", "n")
line = line.replace("%C3%B0", "e")
line = line.replace("%C3%AC", "i")
line = line.replace("%C3%AD", "i")
line = line.replace("%C3%AE", "i")
line = line.replace("%C3%AF", "i")
line = line.replace("%C3%81","A")
line = line.replace("%C3%82","A")
line = line.replace("%C3%83","A")
line = line.replace("%C3%84","A")
line = line.replace("%C3%85","A")
line = line.replace("%C3%86","AE")
line = line.replace("%C3%87","C")
line = line.replace("%C3%88","E")
line = line.replace("%C3%89","E")
line = line.replace("%C3%8A","E")
line = line.replace("%C3%8B","E")
line = line.replace("%C3%8C","I")
line = line.replace("%C3%8D","I")
line = line.replace("%C3%8E","I")
line = line.replace("%C3%8F","I")
line = line.replace("%C3%90","D")
line = line.replace("%C3%91","N")
line = line.replace("%C3%92","O")
line = line.replace("%C3%93","O")
line = line.replace("%C3%94","O")
line = line.replace("%C3%95","O")
line = line.replace("%C3%96","O")
line = line.replace("%C3%98","O")
line = line.replace("%C3%99","U")
line = line.replace("%C3%9A","U")
line = line.replace("%C3%9B","U")
line = line.replace("%C3%9C","U")
line = line.replace("%C3%9D","Y")
line = line.replace("%C3%9F","B")
line = line.replace("%C3%a0","a")
line = line.replace("%C3%a1","a")
line = line.replace("%C3%a2","a")
line = line.replace("%C3%a3","a")
...
但我需要更多 Rails 方式的解决方案,优雅、简单
【问题讨论】:
喜欢这个URI.decode('jonathan-mor%C3%B3n-16394669')
?
我正在寻找相反的东西,比如 Something.method('jonathan-morón-16394669') 并得到 'jonathan-mor%C3%B3n-16394669'
【参考方案1】:
您可以使用CGI::escape
CGI::escape('jonathan-morón-16394669') # => jonathan-mor%C3%B3n-16394669
您也可以使用 CGI::unescape 反过来
CGI::unescape('jonathan-mor%C3%B3n-16394669') # => jonathan-morón-16394669
【讨论】:
所以两者都有效,谢谢!哪个更好,或者你建议我使用 CGI::escape 还是 URI.encode?span> ***.com/questions/30783879/…. 看起来 CGI::escape 可能更合适,谢谢!【参考方案2】:感谢 razvans!我需要的答案是
URI.encode("jonathan-morón-16394669")
哪个输出
[19] pry(main)> URI.encode("jonathan-morón-16394669")
=> "jonathan-mor%C3%B3n-16394669"
【讨论】:
以上是关于我需要将 'jonathan-morón-16394669' 转换为 'jonathan-mor%C3%B3n-16394669'的主要内容,如果未能解决你的问题,请参考以下文章
PHP Htmlentities函数未使用PDO将字符串编码为数据库