【问题标题】:How can I sensibly strip all trailing hashtags out of an Instagram caption?如何明智地从 Instagram 标题中删除所有尾随标签?
【发布时间】:2015-11-07 17:03:29
【问题描述】:

许多 Instagram 帖子以过多的主题标签结尾,例如:

"This is one of the amazing Mountains you can find in the National Forest Park in #Zhangjiajie #Chinawhich is where James Cameron drew his inspiration for the flying mountains in #Avatar..

Credit: @phototravelnomads 
#pictoura #gydr 
#destinationearth #earthpix #ourlonelyplanet#wonderful_earthLife #timeoutsociety#fantastic_earthpics #liveoutdoors #igglobalclub#awesomeearth #mist_vision #earthdeluxe
# #worldbestgram #mthrworld #fantastic_earth#famouscaptures #destination_wow #dreamlifepix#wonderful_places #igworldclub #ig_global_life
#natureaddict #beautifuldestinations #traveler #guider#locals"

我希望处理字幕以在最后删除主题标签集合,同时保持其余部分完好无损。这样做的好方法是什么?我确信我可以找到一种蛮力的方式,但我希望能对一个优雅的解决方案有所了解。不一定是实际代码。 :)

根据 Burna 的评论进行编辑:预期结果是:

"This is one of the amazing Mountains you can find in the National Forest Park in #Zhangjiajie #Chinawhich is where James Cameron drew his inspiration for the flying mountains in #Avatar..

Credit: @phototravelnomads"

根据 Alan Moore 的回答进行编辑:这非常有效,但并非在所有情况下都适用。例如,如果输入文本是:

"This is one of the amazing Mountains you can find in the National Forest Park in #Zhangjiajie #Chinawhich is where James Cameron drew his inspiration for the flying mountains in #Avatar"

...它会从“#张家界”开始被切断。

我认为可能需要更多的逻辑,也许将字符串拆分为数组;检查它是否以主题标签结尾;如果有,那么有多少;如果超过 X (4?),则从最后一个完整系列中的第一个中删除。

【问题讨论】:

  • 处理后能不能加上预期的结果?
  • 欢迎来到 SO!您的问题被自动格式化破坏了;您可以查看编辑历史以查看我做了什么来修复它(当您使用它时,请查看help page 以查看还有什么可用的)。您应该始终在此处预览您的帖子并进行校对。 ;)
  • @burna 我已经编辑了这个问题,但我认为 Alan Moore 已经回答了它:)
  • @WalterVos 太好了,然后通过接受答案来感谢他的努力:)
  • @burna 在进一步查看 Alan 的答案后,我发现它还没有完全到位。

标签: php regex instagram text-processing


【解决方案1】:

看起来可以这样做:

$result = preg_replace('/#[#\w\s]*\z/', '', $subject);

DEMO

正则表达式匹配一个哈希 (#),后跟零个或多个组成标签的字符加上分隔它们的空格 ([#\w\s]*),然后是字符串的结尾 (\z) .

\w 等价于[A-Za-z0-9_]。如果主题标签中允许使用其他字符,或者如果不允许使用数字,请告诉我,我会更新正则表达式。


更新:如果您想删除所有机器人标签,同时保留合法标签,可能没有可靠的方法——当然不能单独使用正则表达式。但是,这将删除除第一行之外的所有主题标签:

$result = preg_replace('/^(#[#\w\h]+\R)#[#\w\s]*\z/m', '$1', $subject);

DEMO

\h 仅匹配水平空格(空格、制表符、nbsp...),\R 匹配任何行分隔符(\r\n 或任何单个垂直空格字符)。

对于文本中类似主题标签的内容,这不会触及它们,因为它已锚定在文本的末尾。行首锚点(^ 在多行模式下)并不是必需的,但它可以帮助正则表达式的未来读者(包括您自己)理解它的作用。当然,cmets 会提供更多帮助。 ;)

【讨论】:

  • 嗨,艾伦,这很好用,但还是有点粗糙。如果文本不包含“大量主题标签”,但确实以一个或两个主题标签结尾,它们也会被剥离。问题是人们也将主题标签用作单词(呃,人)。例如,如果输入是:“这是您可以在 #Zhangjiajie #China 的国家森林公园中找到的令人惊叹的山脉之一,詹姆斯卡梅隆在 #Avatar 中为飞翔的山脉汲取了灵感”
  • 补充一点:Regex 可能不是最佳选择?
  • Regex 绝对不是 最好的 解决方案;从来都不是。但我认为这对这份工作来说已经足够了。检查我的编辑。
【解决方案2】:

如果我理解正确,以下应该可以工作:

$hashTag="pictoura #gydr 

destinationearth #earthpix #ourlonelyplanet#wonderful_earthLife #timeoutsociety#fantastic_earthpics #liveoutdoors #igglobalclub#awesomeearth #mist_vision #earthdeluxe

 #worldbestgram #mthrworld #fantastic_earth#famouscaptures #destination_wow #dreamlifepix#wonderful_places #igworldclub #ig_global_life

natureaddict #beautifuldestinations #traveler #guider#locals";

echo preg_replace('/(#.*\s*)/','',$hashTag);

输出:

pictoura destinationearth natureaddict

祝你好运!

【讨论】:

  • 感谢 angelcool.net 但这只是将它从第一个主题标签上切断(然后在换行处停止)。见:regex101.com/r/jF1cI5/1
猜你喜欢
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多