php 如何获取符号后的字符串

a0x5cqrl  于 2023-02-15  发布在  PHP
关注(0)|答案(4)|浏览(140)
$data['subject']= "Languages > English"; 

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

但是现在我需要得到“〉”符号后面的单词。我如何修改上面的代码?

luaexgnf

luaexgnf1#

或使用爆炸:

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

jtw3ybtb2#

https://php.net/substr

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

但你应该看看爆炸:https://php.net/explode

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

bmvo0sr53#

如果您需要〉之前和之后的数据,我将使用分解。

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

bvuwiixz4#

你可以这么做,
1.字符串将转换为数组
1.然后保留数组的最后一个值

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

相关问题