$string = "1,2,3"
$ids = explode(',', $string);
var_dump($ids);
Run Code Online (Sandbox Code Playgroud)
回报
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
Run Code Online (Sandbox Code Playgroud)
我需要的值是int类型而不是string类型.有没有比使用foreach循环数组并将每个字符串转换为int更好的方法?
例如,我想从这个字符串中的元素创建一个数组:
$str = 'red, green, blue ,orange';
Run Code Online (Sandbox Code Playgroud)
我知道你可以爆炸并循环它们并修剪:
$arr = explode(',', $str);
foreach ($arr as $value) {
$new_arr[] = trim($value);
}
Run Code Online (Sandbox Code Playgroud)
但我觉得有一种单线方法可以解决这个问题.有任何想法吗?
我有一个问题,我有一个字符串数组,我想在不同的分隔符爆炸.例如
$example = 'Appel @ Ratte';
$example2 = 'apple vs ratte'
Run Code Online (Sandbox Code Playgroud)
我需要一个在@或vs.中爆炸的阵列
我已经写了一个解决方案,但如果每个人都有更好的解决方案,请在这里发布.
private function multiExplode($delimiters,$string) {
$ary = explode($delimiters[0],$string);
array_shift($delimiters);
if($delimiters != NULL) {
if(count($ary) <2)
$ary = $this->multiExplode($delimiters, $string);
}
return $ary;
}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,如:
"0123456789"
并需要将EACH字符拆分为数组.
我为它的地狱尝试过:
explode('', '123545789');
Run Code Online (Sandbox Code Playgroud)
但它给了我显而易见的:警告:在爆炸中没有定义分隔符) ..
我怎么会遇到这个?我看不到任何方法,特别是只是一个功能
我有一个PHP数组,我想在前面添加一个减号( - ).我认为通过使用爆炸和内爆它是可能的,但我的PHP知识是不可能实际做到的.任何帮助,将不胜感激.
基本上我想从这个:
$array = [1, 2, 3, 4, 5];
Run Code Online (Sandbox Code Playgroud)
对此:
$array = [-1, -2, -3, -4, -5];
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
如何爆炸以下字符串:
Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor
Run Code Online (Sandbox Code Playgroud)
成
array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor")
Run Code Online (Sandbox Code Playgroud)
因此引用中的文本被视为单个单词.
这就是我现在所拥有的:
$mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor"
$noquotes = str_replace("%22", "", $mytext");
$newarray = explode(" ", $noquotes);
Run Code Online (Sandbox Code Playgroud)
但我的代码将每个单词分成一个数组.如何将引号内的单词视为一个单词?
我正在寻找相当于js 'this is a string'.split('')中PHP的内容.
如果我尝试$array = explode('', 'testing');我得到一个错误Warning: explode() [function.explode]: Empty delimiter in
还有另一种方法吗?
我想explode为所有人写一个字符串:
但这不起作用:
$keywords = explode("\n\t\r\a,-", "my string");
Run Code Online (Sandbox Code Playgroud)
怎么做?