PHP:str_replace()似乎不起作用,为什么?

时间:2014-08-22 07:00:38

标签: php replace foreach str-replace simple-html-dom

我的HTML字符串有两个table元素和一些其他元素。我使用simple_html_dom将表提取到数组中,然后我想从字符串中删除表。

但是str_replace功能似乎根本不起作用。代码如下:

<?php

$string = '<div>
    <div><p>
        What is your first question? What is your first question?What is your first question? What is your first question? What is your first question? What is your first question? What is your first question? What is your first question? What is your first question? What is your first question?
    </p></div>
    <div>
        <table id="table_one">
           <tbody>
            <tr>
              <th style="border:1px solid black; border-collapse:collapse;">
                 <label>COL 1</label>
              </th>
            </tr>
            <tr>
              <td style="border:1px solid black; border-collapse:collapse;">
                 <label>data one</label>
              </td>
            </tr>
            <tr>
              <td style="border:1px solid black; border-collapse:collapse;">
                 <label>data two</label>
              </td>
            </tr>
            <tr>
              <td style="border:1px solid black; border-collapse:collapse;">
                 <label>data three</label>
              </td>
            </tr>
          </tbody>
        </table>
    </div>
    <div>
        <p>Blah Blah Blah</p>
    </div>
    <div>
        <table id="table_two">
          <tbody>
            <tr>
              <th style="border:1px solid black; border-collapse:collapse;">
                 <label>COL 2</label>
              </th>
            </tr>
            <tr>
              <td>
                 <label>data one</label>
              </td>
            </tr>
            <tr>
              <td>
                 <label>data two</label>
              </td>
            </tr>
            <tr>
              <td>
                 <label>data three</label>
              </td>
            </tr>
          </tbody>
        </table>
    </div>
</div>';

require('C:/xampp/htdocs/simple_html_dom.php');
$html = str_get_html($string); // load the string
foreach ($html->find('table') as $node_table) {
    $str_node_table = '';
    $str_node_table = $node_table;
    $tables[] = $str_node_table;
    echo $str_node_table;
    echo str_replace($str_node_table,'',$string);
    echo '---------------------------------------------------------------------------------------------------------------------';
}

问题是为什么?以及如何解决这个问题?您可以在以下屏幕截图中看到输出:

enter image description here

PS: - 我尝试了处理同样问题的其他答案。没有任何帮助!

2 个答案:

答案 0 :(得分:1)

你不需要一个html Dom Parser。

$string = "<table></table><table></table>";
$string = preg_replace('(<table.*?id="([^<]*)".*?>([^<]*)<\/table>)', '', $string);

http://regex101.com/r/sM5lG8/1

这是我的考验。

$string = "<table>d adiasd aid iadas ad</table>d adiasd aid iadas ad<table>d adiasd aid iadas ad</table>";

var_dump($string);
echo '<br />';
$string = preg_replace('(<table>([^<]*)</table>)', '', $string);
echo '<br />';
var_dump($string);

string '<table>d adiasd aid iadas ad</table>d adiasd aid iadas ad<table>d adiasd aid iadas ad</table>' (length=93)

string 'd adiasd aid iadas ad' (length=21)

答案 1 :(得分:0)

Plaese试试这个

echo str_get_html(str_replace($str_node_table,'',$string));