PHP / MySQL preg_match 以哈希符号开头的单词

发布于 2024-12-02 08:36:04 字数 1016 浏览 1 评论 0原文

我为我的网站设计了一个标记系统,其中以哈希 (#) 开头的标记的功能与不带哈希 (#) 的标记不同。 我正在尝试从数据库中提取所有哈希标签并将它们加载到数组中:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里兽 2024-12-09 08:36:04

使用 preg_match_all

$arr = array('#test1, test','#test1, #test2, #test4','#test3, #est5','#test3');
$stack = array();
foreach($arr as $wrds) {
    $val = preg_match_all("/#\w+(?=,|$)/", $wrds, $matched);
    while (!empty($matched[0])) {
        $val = array_pop($matched[0]);
        if (array_search($val, $stack) === FALSE)
        {
            array_push($stack, $val);
        }
    }
}
print_r($stack);

输出:

Array
(
    [0] => #test1
    [1] => #test4
    [2] => #test2
    [3] => #est5
    [4] => #test3
)

use preg_match_all :

$arr = array('#test1, test','#test1, #test2, #test4','#test3, #est5','#test3');
$stack = array();
foreach($arr as $wrds) {
    $val = preg_match_all("/#\w+(?=,|$)/", $wrds, $matched);
    while (!empty($matched[0])) {
        $val = array_pop($matched[0]);
        if (array_search($val, $stack) === FALSE)
        {
            array_push($stack, $val);
        }
    }
}
print_r($stack);

output:

Array
(
    [0] => #test1
    [1] => #test4
    [2] => #test2
    [3] => #est5
    [4] => #test3
)
猛虎独行 2024-12-09 08:36:04

试试这个正则表达式: preg_match("/^\#\w+$/", $wrds, $matched);

try this regexp: preg_match("/^\#\w+$/", $wrds, $matched);

把时间冻结 2024-12-09 08:36:04

正如 @NullUserException 所说,将序列化值放入 RDBMS 中是糟糕的设计,这样做只会让事情变得复杂。

对于你的问题,你可以尝试另一种方式:

$result = array_filter(explode(',', $wrds), function($a){ return $a[0]==='#' } );

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:

$result = array_filter(explode(',', $wrds), function($a){ return $a[0]==='#' } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文