如何使用preg_match php从字符串中获取值?
以下代码。我试图在08-dec-2021
和Expiration Date:
>>> Last update of whois
我试图测试我的代码但没有工作。 (不回应任何东西并显示错误
Warning: preg_match(): No ending delimiter '/' found in on line 3
)
<?php
$test_text = "Updated Date: 04-feb-2014 Creation Date: 08-dec-2009 Expiration Date: 08-dec-2021 >>> Last update of whois >>> database: Thu, 21 Apr 2016 03:56:01 GMT";
preg_match('/Expiration Date: "(.+?)" >>> Last update of whois database',$test_text,$matches);
echo $matches[1];
?>
我该怎么做?
答案 0 :(得分:0)
你必须改变3件事
的 1 即可。从正则表达式中忽略""
的 2 即可。你没有在正则表达式的末尾添加“/
”
<强> 3 即可。将“Last update of whois database
”更改为“Last update of whois >>> database"
”
从正则表达式中忽略""
<?php
$test_text = "Updated Date: 04-feb-2014 Creation Date: 08-dec-2009 Expiration Date: 08-dec-2021 >>> Last update of whois >>> database: Thu, 21 Apr 2016 03:56:01 GMT";
preg_match('/Expiration Date: (.+?) >>> Last update of whois >>> database/',$test_text,$matches);
print_r($matches);
?>
正在使用 Example
答案 1 :(得分:0)
您可以使用preg_match使用以下代码执行此操作
$test_text = "Updated Date: 04-feb-2014 Creation Date: 08-dec-2009 Expiration Date: 08-dec-2021 >>> Last update of whois >>> database: Thu, 21 Apr 2016 03:56:01 GMT";
preg_match('/Expiration Date: (.+?) >>>/',$test_text,$matches);
echo $matches[1];
或者您也可以通过substr函数尝试下面的代码
$test_text = "Updated Date: 04-feb-2014 Creation Date: 08-dec-2009 Expiration Date: 08-dec-2021 >>> Last update of whois >>> database: Thu, 21 Apr 2016 03:56:01 GMT";
$pos=strrpos($test_text,"Expiration Date")+strlen("Expiration Date: ");
echo substr($test_text,$pos,11);