PHP正则表达式

el_*_*_le 3 php regex

我想在没有年份的路径中捕获最后一个文件夹.对于这个字符串路径,我只需要'Millers Crossing'而不是'Movies\Millers Crossing',这是我目前的正则表达式捕获的.

G:\ Movies\Millers Crossing [1990]

preg_match('/\\\\(.*)\[\d{4}\]$/i', $this->parentDirPath, $title);
Run Code Online (Sandbox Code Playgroud)

Fel*_*ing 5

如何basename [文件]substr [文件],而不是复杂的表达式?

$title = substr(basename($this->parentDirPath), 0, -6);
Run Code Online (Sandbox Code Playgroud)

这假设[xxxx]字符串末尾的格式总是有一年.

(它也适用于*nix;))

更新:您仍然可以使用basename获取文件夹,然后应用正则表达式:

$folder = basename($this->parentDirPath);
preg_match('#^(.*?)(?:\[\d{4}\])?$#', $str, $match);
$title = $match[1];
Run Code Online (Sandbox Code Playgroud)