-2

我想使用 PHP 来替换文本字符串中带有哈希标签的单词。

我的字符串:

"Working on some cool things for shareit.me #ui #webdesign #ux"

我想用 span 标签封装每个带有哈希标签的关键字,以赋予它们不同的颜色。我怎样才能做到这一点?

谢谢!

4

2 回答 2

0

如果你想要每个标签不同的颜色

str_replace(array('#ui', '#webdesign', '#ux'), array('<span style="color:red">#ui</span>', '<span style="color:green">#webdesign</span>', '<span style="color:blue">#ux</span>'), "Working on some cool things for shareit.me #ui #webdesign #ux");

如果你想替换所有#tag

preg_replace('/#(\w\d+?)/', '<span style="color: red">#$1</span>', "Working on some cool things for shareit.me #ui #webdesign #ux");
于 2013-04-13T05:35:33.517 回答
0

尝试使用“preg_replace”之类的

preg_replace('/(^|\s)#(\w+)/','<span style="color:green;">\2</span>',$my_string);
于 2013-04-13T05:31:36.750 回答