简体   繁体   中英

How to get value from string using preg_match php?

How to get value from string using preg_match php ?

This below code. I tried to get value 08-dec-2021 between Expiration Date: and >>> 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 )

<?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 . Omit "" from regex
2 . You did not put " / " at the end of regex
3 . Change " Last update of whois database " to " 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

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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