-1

i not understandy i am getting the error preg_match(): Unknown modifier '2' here is my code i trying to parse the audio format.

/**
 * @param string $name
 * @return string|null
 */
public static function parseAudioFormat(string $name): ?string
{
    $Audios = ['AAC', 'AC-3', 'DTS', 'DTS-HD Master Audio', 'DTS-ES', 'DTS 96/24', 'E-AC-3', 'FLAC', 'MP2', 'MP3', 'Opus', 'PCM', 'TrueHD', 'Atmos', 'Vorbis'];
    foreach ($Audios as $audio) {
        if (preg_match("/\b($audio)\b/i", $name, $m)) {
            return $m[0];
        }

    }
    return false;
}
Rio
  • 65
  • 1
  • 8
  • Through the power of substitution :: f($audio)= "/\b($audio)\b/i", f(DTS 96/24)= "`/`\b(DTS 96`/`24)\b`/`i" ,, we see that it is not a metachar problem, but a delimiter problem. –  Oct 02 '20 at 23:13

2 Answers2

0

This is because you are using what are called meta-characters in your search strings and must be escaped in order to be used literally.

0

The forward slash needs to be escaped ('DTS 96/24').

You can do it automatically using preg_quote() :

foreach ($Audios as $audio) {
    if (preg_match("/\b(" . preg_quote($audio) . ")\b/i", $name, $m)) {
        return $m[0];
    }

}
Cid
  • 14,968
  • 4
  • 30
  • 45