在PHP中使用每个空格分隔字符串 [英] Splitting up a string in PHP with every blank space

查看:287
本文介绍了在PHP中使用每个空格分隔字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入,用户可以输入多个单词,然后告诉他们用空格分隔.因此输入可能如下所示:

I have an input where a user may type in multiple words, and they are told to separate it with a space. So input may look like this:

foo

或类似这样:

foo bar php js

如何检查空格,如果有空格,请将单词拆分,然后将其全部放入数组中?我将在程序中遍历该数组.我只是这种字符串处理的新手.

How can I check for spaces, and if there are spaces, split the words, then put it all into an array? I'll loop through that array in my program. I'm just new to string handling like this.

推荐答案

请参见 explode

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

这篇关于在PHP中使用每个空格分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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