Regex remove year from PHP string

I created a database of cigarettes and trading cards. Each heading has a year associated with it. For example, 1943or 2011. A year is always 4 characters, but can be anywhere in the line.

Can someone please help me create a regex that will find the year in a string. I tried '/d{4}\b/', but it does not work.

+5
source share
5 answers
(19|20)[0-9][0-9]

This will only appear on dates ranging from 1900 and 2000.

+7
source

Try the following:

/\b\d{4}\b/

it will match 4 digits enclosed with non-words

+5
source

d{4}\b d . : \d{4}\b. (\b) .

+1
source

This also works. preg_match ("/ ^ 1 [0-9] {3} $ /", $ value))

Checks the year only starting with 1. You can change according to your requirements.

0
source

Here is the complete solution:

$stringWithYear = '1990 New York Marathon';
$stringNoYear = preg_replace('/(19|20)[0-9][0-9]/', '', $stringWithYear);
echo trim($stringNoYear);  // outputs 'New York Marathon'
0
source

All Articles