我有电影片名
The Abandoned 2015 480p Reformed
The Lady in the Car with Glasses and a Gun 2015 BluRay 720p
Rise of the Footsoldier Part II (2015) 720p
我想要的只是电影名称和年份,例如第一个标题The Abandoned 2015
。我有一个正则表达式,在标题示例中找到它在所有上述标题中返回2015年,当我使用strpos(movietitle,year)
返回标题中一年的位置但是当我使用substr(movietitle,0,-(yearpos))
时没有给我一个标题和年份不包括任何内容。
任何有办法做到这一点的人。有许多标题具有不同的字符串长度。
这是我的脚本试图获得其中一个的标题和年份,但没有给我我需要的东西..
$str = "Suffragette 2015 DVDSCR Webz";
if (preg_match('/(^|\s)(\d{4})(\s|$)/', $str, $matches)) {
$year = $matches[0];
}else{
$year = "";
}
if($year != ""){
$pos = strpos($str,$year);
echo substr($str,0,-($pos)); //Fail...
}
答案 0 :(得分:0)
想出了这个解决方案..
$movt = "Paranormal Activity The Ghost Dimension 2015 720p BluRay x265 HEVC RMTeam";
$arrz = array("(",")");
$str = str_replace($arrz,'',$movt);
if (preg_match('/(^|\s)(\d{4})(\s|$)/', $str, $matches)) {
$year = $matches[0];
}else{
$year = "";
}
if($year != ""){
$pos = strpos($str,$year);
$ext = strlen(substr($str,($pos+5)));
echo substr($str,0,strlen($str)-$ext);
}
给出
Paranormal Activity The Ghost Dimension 2015
答案 1 :(得分:0)
试试这个:
$str = "Suffragette 2015 DVDSCR Webz";
if (preg_match('/(^|\s)(\d{4})(\s|$)/', $str, $matches)) {
$year = $matches[0];
}else{
$year = "";
}
if($year != ""){
$pos = strpos($str,$year);
// Add 5 to $pos because " 2015" have 5 characters
echo substr($str,0, (5 + $pos));
}
这适用于您的示例...结果将是Suffragette 2015
,但如果您的标题上有任何年份,则脚本的结果将是不完整的标题。
问候。
答案 2 :(得分:0)
我用你已经掌握的三件事来测试这个,似乎对他们有效(授予它只有3个测试用例)
<?php
$titles = array(
"The Abandoned 2015 480p Reformed",
"The Lady in the Car with Glasses and a Gun 2015 BluRay 720p",
"Rise of the Footsoldier Part II (2015) 720p"
);
//List of titles to look through.
$reg = '/([\w\s]+)\(?([0-9]{4})\)?/';
foreach($titles as $title){
if(preg_match($reg,$title,$matches)){
//If we get a match, keep going
echo $matches[1]. ' ' . $matches[2].'<br />';
}
}
?>