PHP 在下划线之前获取字符串中的所有内容 [英] PHP get everything in a string before underscore

查看:35
本文介绍了PHP 在下划线之前获取字符串中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这个代码:

$imagePreFix = substr($fileinfo['basename'], strpos($fileinfo['basename'], "_") +1);

这让我得到了下划线之后的所有内容,但我希望获得下划线之前的所有内容,我将如何调整此代码以获取下划线之前的所有内容?

this gets me everything after the underscore, but I am looking to get everything before the underscore, how would I adjust this code to get everything before the underscore?

$fileinfo['basename'] 等于 'feature_00'

$fileinfo['basename'] is equal to 'feature_00'

谢谢

推荐答案

你应该简单使用:

$imagePreFix = substr($fileinfo['basename'], 0, strpos($fileinfo['basename'], "_"));

我认为没有任何理由使用 explode 并创建额外的数组来获取第一个元素.

I don't see any reason to use explode and create extra array just to get first element.

您也可以使用(在 PHP 5.3+ 中):

You can also use (in PHP 5.3+):

$imagePreFix = strstr($fileinfo['basename'], '_', true); 

这篇关于PHP 在下划线之前获取字符串中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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