112233
112233

Reputation: 2466

How to get string after a symbol

$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?

Upvotes: 2

Views: 7196

Answers (4)

Sylvain Martin
Sylvain Martin

Reputation: 2375

You can do this way,

  1. your string is converted in an array
  2. then you keep the last value of your array
$data['subject']= "Languages > English";
$subject = end(explode('>',$data['subject']));

Upvotes: 0

Ianis
Ianis

Reputation: 1175

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];

Upvotes: 4

Wouter
Wouter

Reputation: 61

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

Upvotes: 1

Vincent Decaux
Vincent Decaux

Reputation: 10714

Or using explode :

$array = explode(' > ', $data['subject']);
echo $array[0]; // Languages
echo $array[1]; // English

Upvotes: 4

Related Questions