English 中文(简体)
PHP / MySQL preg_matchword成像
原标题:PHP / MySQL preg_match words beginning with hash symbol

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("/#w+(?=,|)/", $wrds, $matched);
    while (!empty($matched))
    {
        $val = array_pop($matched);
        if (array_search($val, $stack) === FALSE)
        {
            array_push($stack, $val);
    }
    }
}

5. MySQL询问如下:

+------------------------+
| Keywords               |
+------------------------+
| #test1, test           |
| #test1, #test2, #test4 |
| #test3, #est5          |
| #test3                 |
+------------------------+

我想像以下几个方面:

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

我做了什么错误?

最佳回答

使用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
)
问题回答

例:preg_match ("/^#w+$/$/>,wrds,$matelle/code>

正如@NullUserException所说,在RDBMS中,设计设计错误,使序列值变得复杂。

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

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...