如何在符号后面获取字符串

时间:2015-08-05 08:54:53

标签: php

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

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

但现在我需要在'>'之后得到消息。符号。如何更改上面的代码?

4 个答案:

答案 0 :(得分:4)

http://php.net/manual/fr/function.substr.php

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

但你应该看看爆炸:http://php.net/manual/fr/function.explode.php

$levels = explode(" > ", $data['subject']);
$subject = $levels[0];
$language = $levels[1];

答案 1 :(得分:4)

或使用爆炸:

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

答案 2 :(得分:1)

如果您想要>之前和之后的数据,我会使用爆炸。

$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

答案 3 :(得分:0)

你可以这样做,

  1. 您的字符串将以数组
  2. 转换
  3. 然后保留数组的最后一个值

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