代码之家  ›  专栏  ›  技术社区  ›  Maurice69

PHP str_是否用通配符替换擦掉的内容?

  •  1
  • Maurice69  · 技术社区  · 6 年前

    我正在寻找一个解决方案,从一个刮掉的HTML页面上剥离一些HTML。这个页面有一些重复的数据我想删除,所以我试着用preg_replace()删除变量数据。

    Producent:<td class="datatable__body__item" data-title="Producent">Example
    Groep:<td class="datatable__body__item" data-title="Produkt groep">Example1
    Type:<td class="datatable__body__item" data-title="Produkt type">Example2
    .... 
    ...
    

    以后一定是这样的:

    Producent:Example
    Groep:Example1
    Type:Example2
    

    所以除了数据标题中的单词外,一个大片段是一样的。我怎样才能删除这段数据?

    我试了一些像这样的方法:

    $pattern = '/<td class=\"datatable__body__item\"(.*?)>/';
    $tech_specs = str_replace($pattern,"", $tech_specs);
    

    3 回复  |  直到 6 年前
        1
  •  0
  •   Rafael    6 年前

    假设字符串是这样的:

    $string = 'Producent:<td class="datatable__body__item" data-title="Producent">Example';
    

    您可以使用以下命令获取字符串的开头和结尾:

    preg_match('/^(\w+:).*\>(\w+)/', $string, $matches);
    
    echo implode([$matches[1], $matches[2]]);
    

    在这种情况下 产品:示例 . 因此,可以将此输出添加到要使用的另一个变量/数组中。 或者,自从你提到 更换 :

    $string = preg_replace('/^(\w+:).*\>(\w+)/', '$1$2', $string);
    

    $string = 'Producent:<td class="datatable__body__item" data-title="Producent">Example
    Groep:<td class="datatable__body__item" data-title="Produkt groep">Example1
    Type:<td class="datatable__body__item" data-title="Produkt type">Example2';
    
    $stringRows = explode(PHP_EOL, $string);
    
    $pattern = '/^(\w+:).*\>(\w+)/';
    $replacement = '$1$2';
    foreach ($stringRows as &$stringRow) {
        $stringRow = preg_replace($pattern, $replacement, $stringRow);
    }
    
    $string = implode(PHP_EOL, $stringRows);
    

    它将像您期望的那样输出字符串。

    第一组抓住 第一 : ,然后另一个组捕获 一言为定。我之前已经为两端指定了锚,但是当断开每一行时,这不会像预期的那样工作,所以我只保留了开头。

    ^(\w+:) => the word in the beginning of the string until two dots appear
    .*\>    => everything else until smaller symbol appears (escaped by slash)
    (\w+)   => the word after the smaller than symbol 
    
        2
  •  0
  •   Maurice69    6 年前

    也许我的问题写得不好。我有一张桌子,我需要从一个网站上刮下来。我需要表中的信息,但必须清理前面提到的一些部分。我最终的解决方案是这个,而且很有效。它仍然有一些工作与手动更换,但那是因为愚蠢的“他们使用英寸”。;-)

    解决方案:

       \\ find the table in the sourcecode
       foreach($techdata->find('table') as $table){
    
        \\ filter out the rows
        foreach($table->find('tr') as $row){
    
        \\ take the innertext using simplehtmldom
        $tech_specs = $row->innertext;
    
        \\ strip some 'garbage'
        $tech_specs = str_replace("  \t\t\t\t\t\t\t\t\t\t\t<td class=\"datatable__body__item\">","", $tech_specs);
    
        \\ find the first word of the string so I can use it    
        $spec1 = explode('</td>', $tech_specs)[0];
    
        \\ use the found string to strip down the rest of the table
        $tech_specs = str_replace("<td class=\"datatable__body__item\" data-title=\"" . $spec1 . "\">",":", $tech_specs);
    
        \\ manual correction because of the " used
        $tech_specs = str_replace("<td class=\"datatable__body__item\" data-title=\"tbv Montage benodigde 19\">",":", $tech_specs);
    
        \\ manual correction because of the " used
        $tech_specs = str_replace("<td class=\"datatable__body__item\" data-title=\"19\">",":", $tech_specs);
    
        \\ strip some 'garbage'
        $tech_specs = str_replace("\t\t\t\t\t\t\t\t\t\t","\n", $tech_specs);
        $tech_specs = str_replace("</td>","", $tech_specs);
        $tech_specs = str_replace("  ","", $tech_specs);
    
        \\ put the clean row in an array ready for usage
        $specs[] = $tech_specs;
        }
      }
    
        3
  •  0
  •   pguardiario    6 年前

    只需使用通配符:

    $newstr = preg_replace('/<td class="datatable__body__item" data-title=".*?">/', '', $str);
    

    .*?