简体   繁体   中英

WP/PHP, how to use regex to get a filename

Working on a Wordpress project with a CPT "audio-book", on the each audio-book post page in the front-end view, it has a mp3 player.

Because there are so many audio files, instead of attaching each audio to its belonging post from the admin, I am wondering how to make a php function that can automatically recognize the right filename and making it available to the front-end.

All the audio files are named under the same rules:

{Category#}-{Story#}-{VoiceOverArtist}_{otherText}.mp3

C{#}-S{#}-{NAME}_{other_text}.mp3 Where the "other_text" part is optional

Here's what the audio filenames are like:

[AudioFolder]

  • C1-S1-Jason_slow.mp3
  • C1-S25-Jenny_revision2.mp3
  • C2-S43-Jason.mp3
  • C4-S99-Thomas_with_background_music.mp3

In the single audio CPT template, I would like a php function "get_audio_file($category, $story_id)".

Ideally:

<?= get_audio_file("C1", "S25"); ?> 

would output "C1-S25-Jenny_revision2.mp3", and

<?= get_audio_file("C4", "S99"); ?> 

would output "C4-S99-Thomas_with_background_music.mp3"

I would also like the function to recognize the voice over artist name, so I can later use a array to cross reference it, and display pink or blue color in the audio post page to indicate the gender of the voice artist.

Here is my current effort with my limited PHP knowledge:

    function get_audio_file($category, $story_id) {
    
    // Define the root folder of the audio files 
    $upload_dir = wp_upload_dir();
    $dir = $upload_dir['baseurl'] . '/story/audio';
    

    // Saw this from other question.. not sure how to implement it with regular expression 
    $files = glob($dir.'/*.mp3');


    $artist = {artist_name}; //this part I have no clue how to grab the artist name into the variable.

    // Getting the filename prefix, eg: C1-S5-{artist}-{suffix}
    // suffix is optional, might not exist
    $filename_prefix = $category . "-" . $story_id;

    if ( !empty( $suffix ) {
      $filename = $filename_prefix . "-" . $artist . "_" . $suffix;
    } else {
      $filename = $filename_prefix . "-" . $artist;
    }

    // Put the voice artist name into gender-based array
    $female_artist = array("Jenny", "Sabrina"...);
    $male_artist = array("Jason", "Tom", "Thomas"...);
    
    
    // This determine the gender of the voice artist 
    if ( in_array( $artist, $female_artist ) ) {
        
        $gender = "female"; 
    
        } elseif ( in_array( $artist, $fale_artist ) ) {
            $gender ="male";
        } else {
            $gender = "null";
        }
    }

 return $filename;

The part I am totally lose is how to use regular expression to assign the correct value into the filename variables...

Can someone please guide me

You may use

(?P<C>C\d+)-(?P<S>S\d+)-(?P<Name>[^_]+)(?P<other>_[^.]+)?\.mp3

See a demo on regex101.com .


Broken down, this reads

(?P<C>C\d+)-       # C, followed by digits and "-"
(?P<S>S\d+)-       # S, followed by digits and "-"
(?P<Name>[^_].+)   # the name part, anything not an underscore
(?P<other>_[^.]+)? # optional part, anything not a dot
\.mp3              # ".mp3" literally

You see, the expression makes the assumption that names do not have underscores (eg Jennifer_Lopez will lead to unexpected results) and that the optional part does not involve dots.


In PHP this could be:

<?php

$files = ["C1-S1-Jason_slow.mp3", "C1-S25-Jenny_revision2.mp3", 
            "C2-S43-Jason.mp3", "C4-S99-Thomas_with_background_music.mp3",
            "C5-S80-Jennifer_Lopez.mp3"];

$regex = "~(?P<C>C\d+)-(?P<S>S\d+)-(?P<Name>[^_\n]+)(?P<other>_[^.]+)?\.mp3~";

$needle = "Jason";
foreach ($files as $file) {
    if (preg_match($regex, $file, $match)) {
        if ($match["Name"] == $needle) {
            echo "Yes: {$match[0]}\n";
        }
    }
}
?>

And would yield

Yes: C1-S1-Jason_slow.mp3
Yes: C2-S43-Jason.mp3

You can compare your needle(s) with $match which is an associative array with the correct values on each iteration. See a working demo on ideone.com .

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