1

我有一些作为 $data 提供给我的数据,其中一些数据的示例是......

<div class="widget_output">
<div id="test1">
    Some Content
</div>
    <ul>
        <li>
            <p>
                <div>768hh</div>
                <div>2308d</div>
                <div>237ds</div>
                <div>23ljk</div>
            </p>
       </li>
        <div id="temp3">
            Some more content
        </div>
       <li>
            <p>
                <div>lkgh322</div>
                <div>32khhg</div>
                <div>987dhgk</div>
                <div>23lkjh</div>
            </p>
        </li>
</div>

我正在尝试更改段落内的无效 HTML DIV,所以我最终得到了这个...

   <div class="widget_output">
<div id="test1">
    Some Content
</div>
    <ul>
        <li>
            <p>
                <span>768hh</span>
                <span>2308d</span>
                <span>237ds</span>
                <span>23ljk</span>
            </p>
       </li>
        <div id="temp3">
            Some more content
        </div>
       <li>
            <p>
                <span>lkgh322</span>
                <span>32khhg</span>
                <span>987dhgk</span>
                <span>23lkjh</span>
            </p>
        </li>
</div>

我正在尝试使用 str_replace 来执行此操作,例如...

$data = str_replace('<div>', '<span>', $data);
$data = str_replace('</div>', '</span', $data);

有没有办法可以将这两个语句结合起来并使其仅影响“这是一个随机项目”而不影响其他事件?

4

3 回答 3

4
$data = str_replace(array('<div>', '</div>'), array('<span>', '</span>'), $data);

只要你没有提供任何其他细节,只问:

有没有办法可以将这两个语句结合起来并使其仅影响“这是一个随机项目”而不影响其他事件?

干得好:

$data = str_replace('<div>This is a random item</div>', '<span>This is a random item</span>', $data);
于 2012-07-29T21:54:23.177 回答
1

You'll need to use a regular expression to do what you are looking to do, or to actually parse the string as XML and modify it that way. The XML parsing is almost surely the "safest," since as long as the string is valid XML, it will work in a predictable way. Regexes can at times fall prey to strings not being in exactly the expected format, but if your input is predictable enough, they can be ok. To do what you want with regular expressions, you'd so something like

$parsed_string = preg_replace("~<div>(?=This is a random item)(.*?)</div>~", "<span>$1</span>, $input_string);

What's happening here is the regex is looking for a <div> tag which is followed by (using a lookahead assertion) This is a random item. It then captures any text between that tag and the next </div> tag. Finally, it replaces the match with <span>, followed by the captured text from inside the div tags, followed by </span>. This will work fine on the example you posted, but will have problems if, for example, the <div> tag has a class attribute. If you are expecting things like that, either a more complex regular expression would be needed, or full XML parsing might be the best way to go.

于 2012-07-29T22:20:45.943 回答
0

我对其他答案有点惊讶,我以为有人会发布一个好的答案,但那还没有发生。str_replace在这种情况下还不够强大,而且正则表达式是命中注定的,你需要编写一个解析器

您不必编写完整的 HTML 解析器,您可以作弊。

$in = '<div class="widget_output">
(..)
</div>';

$lines = explode("\n", $in);

$in_paragraph = false;
foreach ($lines as $nr => $line) {
    if (strstr($line, "<p>")) {
        $in_paragraph = true;
    } else if (strstr($line, "</p>")) {
        $in_paragraph = false;
    } else {
        if ($in_paragraph) {
            $lines[$nr] = str_replace(array('<div>', '</div>'), array('<span>', '</span>'), $line);
        }
    }
}
echo implode("\n", $lines);

这里的关键部分是检测你是否在一个段落中。只有当你在一个段落中时,才进行字符串替换。

注意:我在\n不完美的换行符 ( ) 上进行拆分,但在这种情况下有效。您可能想改进这部分。

于 2012-07-30T09:58:10.497 回答