简体   繁体   中英

differentiate between 3 different string outputs

I'm scraping a string from a website where the output either can be a date which could look like 28.18. or a time like 18:00 or a score like 1:2. How can i check whether the scraped string is equal to a date, time or score? A score will never be more than one character.

Live demo

You can use 3 regex patterns to know which pattern is matched with the given string:

<?php
$subject = "1:1";

if(preg_match('/^\d+\.\d+$/', $subject)){ // Ex: 1.1
    echo "date";    
}else if(preg_match('/^\d\:\d$/', $subject)){ // Ex: 1:1
    echo "score";
}else if(preg_match('/^\d{2}\:\d{2}$/', $subject)){ // Ex: 01:00
    echo "time";
}
?>

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