1
$data['subject']= "Languages > English"; 

//This is how I get the string before the symbol '>'.   
$subject = substr($data['subject'], 0, strpos($data['subject'], "> "));

But Now I need to get word after the '>' symbol. How do I alter the code above?

112233
  • 2,346
  • 2
  • 32
  • 81
  • possible duplicate of [Get everything after a certain character](http://stackoverflow.com/questions/11405493/get-everything-after-a-certain-character) – George Aug 05 '15 at 08:56

4 Answers4

4

https://php.net/substr

$subject = substr($data['subject'], strpos($data['subject'], "> "));

But you should have a look at explode : https://php.net/explode

$levels = explode(" > ", $data['subject']);
$subject = $levels[0];
$language = $levels[1];
Syscall
  • 18,131
  • 10
  • 32
  • 49
Ianis
  • 1,155
  • 6
  • 12
4

Or using explode :

$array = explode(' > ', $data['subject']);
echo $array[0]; // Languages
echo $array[1]; // English
Vincent Decaux
  • 8,658
  • 4
  • 40
  • 65
1

If you want the data before and after the >, I would use an explode.

$data['subject'] = "Languages > English";
$data['subject'] = array_map('trim', explode('>', $data['subject'])); // Explode data and trim all spaces 
echo $data['subject'][0].'<br />'; // Result: Languages
echo $data['subject'][1]; // Result: English
Narendrasingh Sisodia
  • 20,667
  • 5
  • 43
  • 53
0

You can do this way,

  1. your string is converted in an array
  2. then you keep the last value of your array

    <?php $data['subject']= "Languages > English"; $subject = end(explode('>',$data['subject']));

Sylvain Martin
  • 2,265
  • 3
  • 12
  • 27