检查字符串中的#符号和紧随其后的字符,然后为该字符串上色


Check string for # symbol and the immediately following characters, then colour that string

如何检查字符串中的哈希标记及其连接字符?

示例:这是我的示例字符串#示例

*我想提取这个:*#示例

然后我想更改找到的字符串的文本颜色

这将用一个span元素包围标签。

$sampleText = 'This is my sample string #example of what I mean. Here is another #test.';
$pattern = '/#[a-zA-Z0-9]*/';
$replacement = '<span class="otherColor">$0</span>';
$updatedText = preg_replace($pattern, $replacement ,$sampleText);
print_r($updatedText);

输出:

这是我的示例字符串<span class="otherColor">#example<第span>页,共页我的意思。这里是另一个<span class="otherColor">#test<span>。