3

这是我的代码:

$sql = "SELECT `description` FROM `auctions` WHERE `description` REGEXP '[0-9]{10}'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
  echo $row["description"];
}

这将返回整个字段description,我只需要获取与 REGEXP 匹配的部分,我该如何使用 php?

4

2 回答 2

1
$sql = "SELECT `description` FROM `auctions` WHERE `description` REGEXP '[0-9]{10}'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    preg_match('/\d{10}/', $row["description"], $match);
    echo $match[0];
}
于 2013-01-22T08:31:58.057 回答
0

尝试这个:

$results = array();
$sql = "SELECT `description` FROM `auctions` WHERE `description` REGEXP '[0-9]{10}'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    // Checks for a match in the description and capture the match to $match
    preg_match("/[0-9]{10}/", $row['description'], $match);

    // Save the complete match (the ten digits) to the results array.
    $results[] = $match[0];
    // Or just echo them
    echo $match[0];
}

顺便说一句,你还应该看看prepared statements,尤其是为了防止SQL注入。

于 2013-01-22T08:35:14.360 回答