2016-09-27 22 views
-2

我有這個字符串如何在php中刪除部分字符串?

NOTEBOOK > ABC 
TABLET > DFG 

我想刪除一切後 '>' 包括 '>'

我想這

$category = substr($categoryGet,0,strrpos($categoryGet.">",">")); 

沒有結果到目前爲止

+0

在換行符上爆炸,然後在「>」上爆炸 – nogad

+0

爲什麼要將'>>「'連接到字符串?它總能找到你添加的字符,而不是字符串中的字符。 – Barmar

回答

1

您可以使用preg_replace

$category = preg_replace('/>[^>]*$/m', '', $category); 

正則表達式匹配>後跟任何非>字符,直到行尾。 m修飾符使得$匹配字符串中每行的結尾。

+0

不知道是否「NOTEBOOK> ABC片」是他想要的返回 – nogad

+0

號nogad,'NOTEBOOK' 'TABLET'這就是我想要的 – Alexxxx

+0

@nogad我修改它使用'm'修飾符行工作。 – Barmar

0
$str="NOTEBOOK > ABC 
TABLET > DFG"; 

$x=explode("\n",$str); //break by lines 

foreach($x as $y){ 
$k=explode('>',$y); 

echo trim($k[0]); //or that ever form you want the output to be 
} 
相關問題