【问题标题】:WP/PHP, how to use regex to get a filenameWP/PHP,如何使用正则表达式获取文件名
【发布时间】:2021-08-12 12:55:27
【问题描述】:

使用 CPT“有声书”处理 Wordpress 项目,在前端视图中的每个有声书帖子页面上,它都有一个 mp3 播放器。

因为有这么多的音频文件,而不是从管理员将每个音频附加到其所属的帖子,我想知道如何制作一个可以自动识别正确文件名并使其可用于前端的 php 函数。

所有的音频文件都按照相同的规则命名:

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

C{#}-S{#}-{NAME}_{other_text}.mp3 其中“other_text”部分是可选的

音频文件名如下:

[音频文件夹]

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

在单音频 CPT 模板中, 我想要一个 php 函数“get_audio_file($category, $story_id)”。

理想情况下:

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

会输出“C1-S25-Jenny_revision2.mp3”,并且

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

将输出“C4-S99-Thomas_with_background_music.mp3”

我还想要识别配音艺术家姓名的功能,这样我以后可以使用一个数组来交叉引用它,并在音频帖子页面中显示粉红色或蓝色以指示配音艺术家的性别。

这是我目前在 PHP 知识有限的情况下所做的工作:

    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;

我完全失去的部分是如何使用正则表达式将正确的值分配给文件名变量......

有人可以指导我吗

【问题讨论】:

    标签: php regex wordpress


    【解决方案1】:

    你可以使用

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

    a demo on regex101.com


    分解,这是

    (?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
    

    您看,表达式假设名称没有下划线(例如Jennifer_Lopez 将导致意外结果)并且可选部分不包含点。


    PHP 这可能是:

    <?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";
            }
        }
    }
    ?>
    

    并且会产生

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

    您可以将您的针与$match 进行比较,$match 是一个关联数组,每次迭代都具有正确的值。见a working demo on ideone.com

    【讨论】:

    • 您好 Jan,非常感谢您的帮助。我对正则表达式很陌生,你的回答很有帮助。
    • 但是如何将正则表达式的不同部分分配到我的变量中?例如,我是否使用以下代码: $artist_pattern = (?P[^_].+); $artist = preg_match($artist_pattern, $filename, $match); ?>
    • @Atimmy:我用一些额外的代码更新了答案。关键是在preg_match(...) 中使用关联数组并比较您的指针。
    • 您好 Jan,再次感谢您。抱歉,也许我的问题不是很清楚。在音频文件夹中,我有数百个音频文件,所以我不会在 php.ini 中列出我所有的文件名。我希望做的是 = get_audio_file("C1", "S25"); ?> 将输出“C1-S25-Jenny_revision2.mp3”。 (php会搜索文件夹中的所有音频文件名,找到文件名以“C1-S25”开头的匹配文件...
    • 我的每个帖子都有一个自定义字段,其中包含类别和故事 ID 信息。所以在我的前端模板中,我设法设置了 $category 和 $story_id 变量。我正在尝试将此功能放置在我的单个 cpt 模板页面中:= get_audio_file($category, $story_id) ?> 并希望它会产生匹配的文件名,因此我可以将该 URL 放入 mp3 播放器
    猜你喜欢
    • 1970-01-01
    • 2012-10-21
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多