简体   繁体   English

如何使用preg_match PHP从字符串获取值?

[英]How to get value from string using preg_match php?

How to get value from string using preg_match php ? 如何使用preg_match php从字符串获取值?

This below code. 这是下面的代码。 I tried to get value 08-dec-2021 between Expiration Date: and >>> Last update of whois 我试图在Expiration Date:之间获取值08-dec-2021 Expiration Date:>>> Last update of whois

I tried to tested my code but not work. 我试图测试我的代码,但无法正常工作。 (not echo anything and show error Warning: preg_match(): No ending delimiter '/' found in on line 3 ) (不回显任何内容并显示错误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];
?>

How can i do that ? 我怎样才能做到这一点 ?

You have to change 3 things 你必须改变三件事
1 . 1 Omit "" from regex 省略正则表达式中的""
2 . 2 You did not put " / " at the end of regex 您没有在正则表达式的末尾添加“ /
3 . 3 Change " Last update of whois database " to " Last update of whois >>> database" 将“ Last update of whois database ”更改为“ Last update of whois >>> database"
Omit "" from regex 省略正则表达式中的""

<?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);
?>

Working Example 工作实例

You can do this as you are doing by preg_match using code below 您可以像使用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];

Or you can also do by substr function try below code 或者你也可以通过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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM