Alexxxx Alexxxx - 2 months ago 36
PHP Question

How to remove part of string in php?

I have this string

NOTEBOOK > ABC
TABLET > DFG


I want to remove everything after '>' including '>'

I tried this

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


No result so far

Answer

You can use preg_replace

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

The regular expression matches > followed by any non-> characters until the end of the line. The m modifier makes $ match the end of each line in the string.