Preg_match()在匹配"空格双引号"时失败.模式(对从MSSQL数据库查询的字符串)


preg_match() fails when trying to match a "space double quote" pattern (on string queried from MSSQL DB)

你能告诉我这是怎么回事吗?

我从MSSQL DB查询了这个字符串

$string = 'Key Lesson: "<u>Part 1</u>';  

看到双引号前的空格了吗?由于某些原因,这三个匹配尝试都失败了:

<?php
if(preg_match('~Key Lesson: "<u>Part 1</u>~i', $string, $matches) == 1)
if(preg_match('~Key Lesson:[[:blank:]]+"<u>Part 1</u>~i', $string, $matches) == 1)
if(preg_match('~Key Lesson:[[:space:]]+"<u>Part 1</u>~i', $string, $matches) == 1)
?>

但是这两个都可以:

if(preg_match('~Key Lesson:'h"<u>Part 1</u>~i', $string, $matches) == 1)
if(preg_match('~Key Lesson:."<u>Part 1</u>~i', $string, $matches) == 1)

里面的字符到底是什么?它是怎么进去的?

我唯一的猜测,考虑到它是一个双引号,字符串是从MSSQL数据库中提取的,它与数据准备有关,当数据存储在DB中或当它被查询使用时。(我使用的是mssql_query() &mssql_fetch_array()获取数据的PHP函数)

你的代码和我一起工作没有任何问题。尝试下面的代码,并检查它是否适用于您:

<?php
$string = 'Key Lesson: "<u>Part 1</u>';  
if(preg_match('~Key Lesson: "<u>Part 1</u>~i', $string, $matches) == 1) echo 'OK 1 ';
if(preg_match('~Key Lesson:[[:blank:]]+"<u>Part 1</u>~i', $string, $matches) == 1) echo 'OK 2 ';
if(preg_match('~Key Lesson:[[:space:]]+"<u>Part 1</u>~i', $string, $matches) == 1) echo 'OK 3 ';
if(preg_match('~Key Lesson:'h"<u>Part 1</u>~i', $string, $matches) == 1) echo 'OK 4 ';
if(preg_match('~Key Lesson:."<u>Part 1</u>~i', $string, $matches) == 1) echo 'OK 5 ';
?>

演示:http://ideone.com/R1CeWy

相关文章: