PHP / MySQL preg_match 以哈希符号开头的单词
我为我的网站设计了一个标记系统,其中以哈希 (#) 开头的标记的功能与不带哈希 (#) 的标记不同。 我正在尝试从数据库中提取所有哈希标签并将它们加载到数组中:
$keywords = mysql_query("SELECT Keywords FROM Tags WHERE Keywords LIKE '#%'") or die("Query failed with error: ".mysql_error());
$stack = array();
while ($row = mysql_fetch_array($keywords))
{
$wrds = $row['Keywords'];
$val = preg_match("/\b\#\w+(?=,|\b)/", $wrds, $matched);
while (!empty($matched))
{
$val = array_pop($matched);
if (array_search($val, $stack) === FALSE)
{
array_push($stack, $val);
}
}
}
MySQL 查询返回以下内容:
+------------------------+
| Keywords |
+------------------------+
| #test1, test |
| #test1, #test2, #test4 |
| #test3, #est5 |
| #test3 |
+------------------------+
我想要一个如下所示的数组:
Array(
[0] => #test1
[1] => #test2
[2] => #test4
[3] => #test3
[4] => #est5
)
我做错了什么?
I've devised a tagging system for my Website where tags beginning with a hash (#) function differently to those without.
I'm trying to extract all hash tags from my database and load them into an array:
$keywords = mysql_query("SELECT Keywords FROM Tags WHERE Keywords LIKE '#%'") or die("Query failed with error: ".mysql_error());
$stack = array();
while ($row = mysql_fetch_array($keywords))
{
$wrds = $row['Keywords'];
$val = preg_match("/\b\#\w+(?=,|\b)/", $wrds, $matched);
while (!empty($matched))
{
$val = array_pop($matched);
if (array_search($val, $stack) === FALSE)
{
array_push($stack, $val);
}
}
}
The MySQL query returns the following:
+------------------------+
| Keywords |
+------------------------+
| #test1, test |
| #test1, #test2, #test4 |
| #test3, #est5 |
| #test3 |
+------------------------+
I want an array like the following:
Array(
[0] => #test1
[1] => #test2
[2] => #test4
[3] => #test3
[4] => #est5
)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
preg_match_all
:输出:
use
preg_match_all
:output:
试试这个正则表达式:
preg_match("/^\#\w+$/", $wrds, $matched);
try this regexp:
preg_match("/^\#\w+$/", $wrds, $matched);
正如 @NullUserException 所说,将序列化值放入 RDBMS 中是糟糕的设计,这样做只会让事情变得复杂。
对于你的问题,你可以尝试另一种方式:
As @NullUserException said, it is bad design fo putting serialized values in RDBMS, doing this just make things complicated.
And for your question, you can try another way: