【问题标题】:How to extract #tags from string? [duplicate]如何从字符串中提取#tags? [复制]
【发布时间】:2019-10-15 13:12:56
【问题描述】:

我想从一个字符串中提取所有#words,例如如果我有这个字符串:

Hello #people how are you doing #today I am in #asia and weather is super #nice

我需要一个 PHP 函数来返回这些:

#people
#today
#asia
#nice

【问题讨论】:

  • 到目前为止你有什么尝试?

标签: php


【解决方案1】:

这是我应用您的请求的示例

$string = "Hello #people how are you doing #today I am in #asia and weather is super #nice";
$regex = '~(#\w+)~';
if (preg_match_all($regex, $string, $matches, PREG_PATTERN_ORDER)) {
   foreach ($matches[1] as $word) {
      echo $word .'<br/>';
   }
}

【讨论】:

  • 我运行了它,它没有显示任何内容。
  • 很抱歉,缺少一个字符。
猜你喜欢
  • 1970-01-01
  • 2021-03-06
  • 2021-05-08
  • 2017-09-24
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多