返回第一次出现在php中的字符之前的字符串部分 [英] Return the portion of a string before the first occurrence of a character in php

查看:315
本文介绍了返回第一次出现在php中的字符之前的字符串部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,最简单的方法是在首次出现特定字符之前返回字符串的一部分?

In PHP, what is the simplest way to return the portion of a string before the first occurrence of a specific character?

例如,如果我有一个字符串...

For example, if I have a string...

快速的棕色狐狸跳过了等等,等等."

"The quick brown foxed jumped over the etc etc."

...并且我正在过滤空格字符("),该函数将返回 "

...and I am filtering for a space character (" "), the function would return "The"

谢谢!

推荐答案

可以这样做:

$string = 'The quick brown fox jumped over the lazy dog';
$substring = substr($string, 0, strpos($string, ' '));

但是我喜欢这个更好:

list($firstWord) = explode(' ', $string);

这篇关于返回第一次出现在php中的字符之前的字符串部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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