There is a line: Главная>Новости>Новость 1 .

It is necessary to cut the line before the last character > , so that only Новость 1 remains.

How to do this with php ?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

You yourself could easily cope with this task if you decompose it into its components (this is called decomposition):

  1. Find the position of the last character searched.
  2. Cut the string after it

     $pos = strrpos($str, '>'); if (!$pos !== false) { $str = substr($str, $pos + 1); } 

or

  1. Split a string into an array of the desired character
  2. Find the length of the array
  3. Get the last (length - 1) array element

     $bits = explode('>', $str); $str = $bits[sizeof($bits) - 1]; 

Bonus - magic odnogoschnik

 return explode('>', $str)[substr_count('>', $str) - 1]; 
  • one
    array_pop(explode('>', $str)) shorter. - Visman
  • @Visman, yeah, I fell out of the industry a bit - etki