Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提要解析、拆分字符串_Php_Sql_Xml - Fatal编程技术网

Php 提要解析、拆分字符串

Php 提要解析、拆分字符串,php,sql,xml,Php,Sql,Xml,假设我有以下几点: $cars = "auto1,auto2,auto3,auto4"; 如果我解析$cars以在数据库中插入值,我会得到一个类似“auto1、auto2、auto3、auto4”的字符串 当我将提要解析为“auto1、auto2、auto3、auto4”时,我想拆分这个字符串,基本上是在单词之间添加空格。我怎么做 下面是我获取$cars值的方法: $xml = simplexml_load_file($feed); foreach( $xml->feedinfo as

假设我有以下几点:

$cars = "auto1,auto2,auto3,auto4";
如果我解析
$cars
以在数据库中插入值,我会得到一个类似“auto1、auto2、auto3、auto4”的字符串

当我将提要解析为“auto1、auto2、auto3、auto4”时,我想拆分这个字符串,基本上是在单词之间添加空格。我怎么做

下面是我获取
$cars
值的方法:

$xml = simplexml_load_file($feed);

foreach( $xml->feedinfo as $feedinfo )
{
    $cars = $feedinfo->cars;
    [...]
}

如果您只想添加空格,谢谢您

$cars = str_replace(',', ', ', $cars);
但您也可以将其拆分为一个元素数组:

$cars = explode(',', $cars)

如果您只想在字符串中插入空格,为什么不简单地将每个
替换为
(逗号+空格)?为什么要使用
foreach
$cars
赋值?那个变量不是每次都会被覆盖吗?为什么不直接获取正确的
$feedinfo