php字符串函数,用于在最后一次出现字符之前获取子字符串 [英] php string function to get substring before the last occurrence of a character

查看:66
本文介绍了php字符串函数,用于在最后一次出现字符之前获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$string = "Hello World Again".
echo strrchr($string , ' '); // Gets ' Again'

现在,我想从$string [最后一次出现空格''之前的子字符串"中获取"Hello World".我怎么得到它??

Now I want to get "Hello World" from the $string [The substring before the last occurrence of a space ' ' ]. How do I get it??

推荐答案

这是一种廉价的方法,但是您可以拆分,弹出然后加入以完成它:

This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:

$string = 'Hello World Again';
$string = explode(' ', $string);
array_pop($string);
$string = implode(' ', $string);

这篇关于php字符串函数,用于在最后一次出现字符之前获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆