【问题标题】:Php looping white explode to stringPHP循环白色爆炸成字符串
【发布时间】:2021-06-21 12:45:57
【问题描述】:

我正在构建一个打包数据的 PHP。

但我遇到了以下问题。 生成一个函数来获取模型名称。

    function model($model){
    $model = explode('"Models"', $model);
    $model =  $model[1];
    
    $model = explode('</div></div>', $model);
    $model =  $model[0];
    
    $model = explode('bycjqw', $model);
    $nr2 = count($model);
    
    for ($e = 1; $e <= $nr2; $e++) 
        {
            $name= explode('<', $model[$e]);
            $name=  $name[0];
    
            $name= str_replace('-0 gtoSow">', '', $name);
    
            if($name != ''){
                echo('1:'.$name.'<br>');
            }
        }
        $models = implode(',', $name);
        echo('2:'.$models);
        return $models;
}

我有1个分割的数据还可以

1:名称_1 1:名称_2 1:名称_3

但在 2 中它是空的。

谁能帮我解压?

【问题讨论】:

  • 看起来您正在尝试使用 HTML,但您的方法将非常脆弱,如果更改原始来源可能会失败。
  • 另外,implode 的第二个参数是 array$name是变量数组吗?
  • 不,name 是一个字符串,而 implode 必须是一个字符串。能够将其保存在数据库中
  • 请打开implode手册,该功能的描述是“implode——用字符串连接数组元素”。所以你显然做错了什么
  • 你应该解释你想用 implode 函数实现什么(因为它显然没有做你认为它做的事情)。

标签: php string loops


【解决方案1】:

认为你想跟踪每个$name,你可以使用另一个数组来做到这一点,我称之为$names,然后是implode。下面是相同的代码,除了上面注释的我添加的两行。

function model($model)
{
    $model = explode('"Models"', $model);
    $model = $model[1];

    $model = explode('</div></div>', $model);
    $model = $model[0];

    $model = explode('bycjqw', $model);
    $nr2 = count($model);

    // Names will be stored here
    $names = [];
    for ($e = 1; $e <= $nr2; $e++) {
        $name = explode('<', $model[$e]);
        $name = $name[0];

        $name = str_replace('-0 gtoSow">', '', $name);

        if ($name != '') {
            echo('1:' . $name . '<br>');
            // Store name
            $names[] = $name;
        }
    }
    $models = implode(',', $names);
    echo('2:' . $models);

    return $models;
}

【讨论】:

  • 谢谢,我没有意识到我需要声明模型 [] ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多