I have a working code for printing just the year (number) if found in the current post excerpt, but the problem arises when the number (a year) is followed by a Japanese character (example: '2023年', or whatever Japanese word follows with no space)

I tried many things like:

if (preg_match('/'.$year.'[^0-9a-zA-Z\s]|[^0-9a-zA-Z\s]'.$year.'[^0-9a-zA-Z\s]/u', $current_excerpt))

trying to strip Unicode characters, but nothing works (my experience and knowledge is limited)

I just want to print that year, from the year's list, if it exist, nothing else, no matter what it is surrounded by or lack of space in between.

many thanks

the function I have:

function custom_year_shortcode() {
    global $post;
    $current_post_id = $post->ID;
    $current_excerpt = get_the_excerpt($current_post_id);
    $allowed_years = array('2018', '2019', '2020', '2021', '2022', '2023', '2024');
    $found_years = array();

    // Search for years in the excerpt
    foreach ($allowed_years as $year) {
        if (preg_match('/\b'.$year.'\b/', $current_excerpt)) {
            $found_years[] = $year;

    // Print only the found years
    $output = implode(', ', $found_years);

    return $output;
add_shortcode('year_shortcode', 'custom_year_shortcode');
  • I suspect the solution to this has nothing to do with WordPress and would be a generic programming problem that works the same everywhere, aka "How do I get an array of all instances of X Y or Z in a string in PHP?". It just happens to be in a shortcode and the string is an excerpt. You'd have gotten a lot more people looking at it if you'd asked it as a general PHP question on stackoverflow instead of limiting yourself to WordPress
    – Tom J Nowell
    Commented Jul 17, 2023 at 14:23
  • also note that the other things you said you tried but couldn't figure out are also good stackoverflow questions that might have already been asked and answered
    – Tom J Nowell
    Commented Jul 17, 2023 at 14:30

1 Answer 1


If you have a strict list of allowed years:

$allowed_years = array('2018', '2019', '2020', '2021', '2022', '2023', '2024');

Then why are you using regex when you can search for those years directly:

function vyperlook_get_years_in_text( string $text, array $allowed_years ) : array {
    $found_years = []; // the years we found
    // for each year that is allowed
    foreach( $allowed_years as $year ) {
        // if it's in the excerpt
        if ( str_contains( $text, $year ) ) {
            // add it to the list
            $found_years[] = $year;
    return $found_years;
$excerpt = '1986 Alice Bob 2020年, 1984 and 2022!';
$years = vyperlook_get_years_in_text( $excerpt, [ '2018', '2019', '2020', '2021', '2022', '2023', '2024' ] );
echo implode(', ', $years);

The above outputs this:

2020, 2022

Unicode/Regex/Japanese/etc can all be ignored. I'm sure there's a variation of the regex that will fix your code, but it's massively overcomplicating the problem. As for why it doesn't work, it's because \b matches zero width boundary positions, and not all unicode values are for whole characters, there's a tonne of nuance that's being ignored.

  • thank you so much, but still doesn't work, it will print nothing. The excerpt does have one of those years, but can it be identified if the year and another character has no space in between them? example: '2023年' - there is no space between '2023' and '年'
    – vyperlook
    Commented Jul 17, 2023 at 15:35
  • do you have an example? There's no test excerpts to check your code or any modifications against, or any indication of how you're checking this
    – Tom J Nowell
    Commented Jul 17, 2023 at 16:07
  • I made some edits to put the code inside a function, a code block testing that function with example data and what the output would be. As far as I can tell it works, if it does not you will need to provide examples of inputs that do not work, and be specific with both what you tried, what you expected, and how it differed. My example works for me in PHP v8.0 even when including characters. Spacing before or after the dates is irrelevant as there are no spaces in the strings in the allowed years array and no regex, it is doing exact comparisons
    – Tom J Nowell
    Commented Jul 17, 2023 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.