Mike Q Mike Q - 1 month ago 8
PHP Question

cut everything after year in php with regex

In PHP I want to cut any excess chars after the year values in php to have it look like this:

"something-else-2014"


Examples of strings before:

"something-else-2003-who knows what "
"something-else-2012asd9&S(AS&(&AS("
"something-else-2014rrrrrrrrrr"


Desired results:

"something-else-2003"
"something-else-2012"
"something-else-2014"

Answer Source

Try this,

$var = "something-else-2003-who knows what ";
preg_match('~(.*?[0-9]{4})~',$var,$match);
print_r($match);
if(!empty($match)){
    echo $match[1];
}