开发者

php excluding everything before a space

开发者 https://www.devze.com 2023-03-15 06:40 出处:网络
i have 9:42 say: 1 <+tk->sherlockholmes: there was scream : why I want to ignore everything before the first space(\" \").

i have

9:42 say: 1 <+tk->sherlockholmes: there was scream : why

I want to ignore everything before the first space(" ").

One method which i thought of was using the explode() function but i am having problem i did this :

$f_a = explode(" ",$tobesplit));
for ($i=1;$i<=count($f_a);$i++)
{
$ff_a .= $f_a[$i];
}
echo $ff_a

But it is giving errors can anyone tell why ?

And if there is a better wa开发者_如何学Pythony of doing this Please tell me

Thanks


Just use strpos [docs] and substr [docs]:

$part = substr($str, strpos($str, ' '));


You could use the strstr() function to get only the portion of the string after the first space, like this:

$portion = strstr($str, ' ');

For more information, take a look at the documentation for the strstr() function.


preg_replace('/^[^ ]+ /', '', $tobesplit, 1);


$i is getting greater than the upper bound of your array. Change

for ($i=1;$i<=count($f_a);$i++)

with

for ($i=1;$i<count($f_a);$i++)

This and Glass Robot's answer.


You have an extra close parenthesis on explode.

The is no need for the loop you can limit the number of results form explode:

list(,$result) = explode(" ",$tobesplit, 2);

echo $result;


There are some errors:

  1. There is a closed parenthesis that you have to leave from first line.
  2. You have to use "<" and not "<=" in for condition.
  3. You have to declare ff_a out of cycle
  4. You have to end with ";" last line.
0

精彩评论

暂无评论...
验证码 换一张
取 消