php 如何在php中将字符串转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5203950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:37:15  来源:igfitidea点击:

how to convert a string to an array in php

php

提问by hunter

how to convert a string in array in php i.e

如何在php中转换数组中的字符串即

 $str="this is string";

should be like this

应该是这样的

 arr[0]=this
      arr[1]=is
      arr[2]=string 

The

str_split($str, 3);
将字符串拆分为 3 个字符的单词,但我想在数组中的空格之后转换字符串。

回答by deceze

$array = explode(' ', $string);

回答by Shakti Singh

With explodefunction of php

具有php的爆炸功能

$array=explode(" ",$str); 

This is a quick example for you http://codepad.org/Pbg4n76i

这是一个简单的例子http://codepad.org/Pbg4n76i

回答by Codemwnci

Take a look at the explodefunction.

看看explode函数。

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

回答by Ravindra

<?php

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

?>

回答by Danijel

There is a function in PHP specifically designed for that purpose, str_word_count(). By default it does not take into account the numbers and multibyte characters, but they can be added as a list of additional characters in the charlistparameter. Charlist parameter also accepts a range of characters as in the example.

PHP 中有一个专门为此目的而设计的函数,str_word_count(). 默认情况下,它不考虑数字和多字节字符,但可以将它们作为附加字符列表添加到charlist参数中。Charlist 参数也接受示例中的一系列字符。

One benefit of this function over explode()is that the punctuation marks, spaces and new lines are avoided.

此功能的一个好处explode()是避免了标点符号、空格和换行符。

$str = "1st example:
        Alte Füchse gehen schwer in die Falle.    ";

print_r( str_word_count( $str, 1, '1..9ü' ) );

/* output:
Array
(
    [0] => 1st
    [1] => example
    [2] => Alte
    [3] => Füchse
    [4] => gehen
    [5] => schwer
    [6] => in
    [7] => die
    [8] => Falle
)
*/

回答by Andreas

explode() might be the function you are looking for

explode() 可能是您正在寻找的函数

$array = explode(' ',$str);

回答by Yevgeniy Afanasyev

try json_decode like so

像这样尝试 json_decode

<?php

   $var = '["SupplierInvoiceReconciliation"]';
   $var = json_decode($var, TRUE);
   print_r($var);
?>

回答by Gautam Rai

here, Use explode() function to convert string into array, by a string

在这里,使用explode()函数将字符串转换为数组,通过一个字符串

click here to know more about explode()

点击这里了解更多关于explode()

$str = "this is string";
$delimiter = ' ';  // use any string / character by which, need to split string into Array
$resultArr = explode($delimiter, $str);  
var_dump($resultArr);

Output :

输出 :

Array
(
    [0] => "this",
    [1] => "is",
    [2] => "string "
)

it is same as the requirements:

它与要求相同:

  arr[0]="this";
  arr[1]="is";
  arr[2]="string";

回答by Amitesh

explode— Split a string by a string

爆炸— 按字符串拆分字符串

Syntax :

句法 :

array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
  • $delimiter : based on which you want to split string
  • $string. : The string you want to split
  • $delimiter :基于您要拆分的字符串
  • $字符串。: 要拆分的字符串

Example :

例子 :

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

In your example :

在你的例子中:

$str = "this is string"; 
$array = explode(' ', $str);

回答by Harshi Srivastava

You can achieve this even without using explode and implode function. Here is the example:

即使不使用爆炸和内爆功能,您也可以实现这一点。这是示例:

Input:

输入

This is my world

这是我的世界

Code:

代码

$part1 = "This is my world"; 
$part2 = str_word_count($part1, 1);

Output:

输出

Array( [0] => 'This', [1] => 'is', [2] => 'my', [3] => 'world');

数组( [0] => '这个', [1] => '是', [2] => '我的', [3] => '世界');