0

我正在寻找一个 php 脚本来检测一大段文本中的 twitter 风格的主题标签。所以它会搜索#then_a_word。

4

2 回答 2

10

使用正则表达式 and 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 函数部分:还有一些其他的可能对您有所帮助。

于 2011-03-10T12:20:19.467 回答
0
<?php

$str = "I LIKE BURGERS"; // for example
$pattern = "\burgers\i";
echo "$pattern, "BURGERS", $str";

?>
于 2020-11-07T15:03:32.713 回答