Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

regex - PHP preg_match parser - How get uppercase letters

I have code that parse copypasted mediainfo text and create the array, but I have a problem because in this code $line needs to be a lower case, otherwise it's not print anything.

How I can make this working with uppercase letters?

If I change this to strtoupper code not working anymore.

$line = trim(strtolower($line));

If I wanna parse filename it's always lowercase.

Example:

Original:

My.Home.Video.S01.E01.mp4

After parse:

my.home.video.s01.e01.mp4

class Parser {

// Mediainfo 
private $regex_section = "/^(?:(?:general|video|audio|text|menu)(?:s#d+?)*)$/i";

public function parse($string)
{
    $string = trim($string);
    $lines = preg_split("/
|
|
/", $string);

    $output = [];
    foreach ($lines as $line) {
        $line = trim(strtolower($line));
        if (preg_match($this->regex_section, $line)) {
            $section = $line;
            $output[$section] = [];
        }
        if (isset($section)) {
            $output[$section][] = $line;
        } else {
            $output['general'][] = $line;
        }
    } 

EDIT:

Whole code: https://pastebin.com/MkxSYk1W

If I remove strtolower from this line $line = trim(strtolower($line));

I got this when I print output. No values.

Array ( [general] => [video] => [audio] => [text] => )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand correctly you would like to group the filenames by section.

$filenames = [
    'My.Home.Video.S01.E01.mp4',
    'my.home.video.s01.e02.mp4',
    'My.Home.Audio.S01.E01.mp4'
];

// match specific strings between dots, capture as "section"
$pattern = '(\.(?<section>general|video|audio|text|menu)\.)i';

$output = [];
foreach ($filenames as $filename) {
    // default section
    $section = 'general';
    if (preg_match($pattern, $filename, $match)) {
        // lowercase captured section
        $section = strtolower($match['section'] ?? 'general');
    }
    if (isset($output[$section])) {
        // add to existing group
        $output[$section][] = $filename;
    } else {
        // add new group
        $output[$section] = [$filename];
    }
    
}

var_dump($output);

Output:

array(2) {
  ["video"]=>
  array(2) {
    [0]=>
    string(25) "My.Home.Video.S01.E01.mp4"
    [1]=>
    string(25) "my.home.video.s01.e02.mp4"
  }
  ["audio"]=>
  array(1) {
    [0]=>
    string(25) "My.Home.Audio.S01.E01.mp4"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...