gpt4 book ai didi

php - 在 PHP 中从一大块文本中检测标签

转载 作者:行者123 更新时间:2023-12-01 00:02:33 24 4
gpt4 key购买 nike

我想创建一个 php 脚本,用于从一大块文本中检测 Twitter 风格的话题标签。所以它会搜索#then_a_word。

最佳答案

使用正则表达式和 preg_match_all() ,你可以有这样的东西:

$str = <<<STR
this is a string
with a #tag and
another #hello one
STR;

$matches = array();
if (preg_match_all('/#([^\s]+)/', $str, $matches)) {
var_dump($matches[1]);
}


给出以下输出:

array
0 => string 'tag' (length=3)
1 => string 'hello' (length=5)


而且,如果您需要对这些进行更多操作,您应该查看 PCRE Functions手册部分:还有一些其他内容可能对您有所帮助。

关于php - 在 PHP 中从一大块文本中检测标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5259469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com