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"
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];
}
.+\d{4}
You can use this regex. Any character at least once, followed by four digits.