在了解我正在进行的项目preg_match()
时,我一直在使用regexr.com来测试我的表达方式。但是,我在regexr.com上得到了一个表达式,一旦我将它实现到preg_match函数中,它就不起作用了。
这是我表达的意思。你看到的蓝色突出显示的确是我想要的。 http://regexr.com/3ccne
这是我的PHP:
$text = "A new quote has been logged in your online quoting system at 3\/16\/2014 10:48:13 PM.\r\n\r\nThe quote is from Cade Carrier; (email: cadecarrier@rocketmail.com) and is for these items:\r\n\r\n\r\nTires: 195 60 15 - Direct Input (2)\r\n\r\nAdditional request information:\r\n\r\nAddress:\r\n412 2nd st \r\nElton, United States Louisiana, 70532\r\nPhone: 2817399840\r\n\r\nLocation Information:\r\n\r\nYou have requested a quote from the following location:\r\nStore 16\r\n810 3rd. Ave\r\nKinder, LA 70648\r\n\r\nComments:\r\n\r\n";
preg_match('/Address:.*(, \d{5})/', $text, $address);
$address
数组为空。是什么给了什么?
答案 0 :(得分:3)
单词Address
和后跟空格和5位数的逗号用换行符号分隔。要强制点匹配换行符,请使用/s
修饰符(我认为懒惰点匹配模式在这里更好,因为从Address
到五位数的逗号比从末尾回溯更快字符串):
'/Address:.*?(, \d{5})/s'
结果:
[0] => Address:
412 2nd st
Elton, United States Louisiana, 70532
[1] => , 70532
如果您不需要Item [1],只需使捕获组不捕获:'/Address:.*?(?:, \d{5})/s'
。