Php 使用regex返回mysql字符串
这是我的密码:Php 使用regex返回mysql字符串,php,mysql,Php,Mysql,这是我的密码: $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实现这一点?
$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实现这一点?尝试以下操作:
$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];
}
$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];
}
顺便说一句,您还应该注意防止SQL注入。您可以将当前输出粘贴到您的问题吗?您必须使用普通PHP执行此操作,因为如果将正则表达式部分移动到select中,它将只返回
0
或1
。(你可以看到一个例子)。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO。