2014-09-23 10 views
-1

我需要的如「2014」(XXXX)的線串匹配,我的意思是:PHP REG前

空間,並在字符串末尾修復4位

The Shawshank Redemption 1994 
The Godfather 1972 
The Dark Knight 2008 
Pulp Fiction 1994 
The Good, the Bad and the Ugly 1966 
Schindler's List 1993 
12 Angry Men 1957 
The Lord of the Rings: The Return of the King 2003 
Fight Club 1999 

我會用這與

$pattren=''; 
$replace=preg_replace($pattren, "", $input_lines); 

首先我嘗試使用爆炸,但沒有作品如我所料,我有不好的訣竅與正則表達式

回答

1

使用這一個:

$replace = preg_replace(/\s+\d{4}$/, "", $input_lines); 
0

使用TRIM()從上刪除空格。 (這將刪除任何數量的空格)

<?php 
     $str = " The Shawshank Redemption 1994  "; 
     $str = trim($str); 
     echo $output = substr($str,-4); 

?>