如何删除PHP中的部分字符串?

时间:2016-09-27 01:19:49

标签: php

我有这个字符串

NOTEBOOK > ABC
TABLET > DFG

我想删除'>'之后的所有内容包括'>'

我试过这个

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

到目前为止没有结果

2 个答案:

答案 0 :(得分:1)

您可以使用preg_replace

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

正则表达式匹配>后跟任何非>个字符,直到行尾。 m修饰符使$与字符串中每行的结尾匹配。

答案 1 :(得分: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
}