正则表达式匹配包含HTML的占位符

时间:2016-07-15 06:42:40

标签: php regex

我有占位符,用户可以插入WYSIWYG编辑器(包含HTML代码)。有时,当他们从Word等应用程序粘贴时,会在其中注入HTML。

例如:它粘贴%<span>firstname</span>%而不是%firstname%

以下是我的正则表达式代码示例:

$html = '

    <p>%firstname%</p>

    <p>%<span>firstname</span>%</p>

    <p>%<span class="blah">firstname</span>%</p>

    <p>%<span><span>firstname</span></span>%</p>

    <p>%<span><span><span>firstname</span></span></span>%</p>

    <p>%<span class="blah"><span>firstname</span></span>%</p>

    <div>other random <strong>HTML</strong> that needs to be preserved.</div>

';

preg_match_all(
    '/\%(?![0-9])((?:<[^<]+?>)?[a-zA-z0-9_-]+(?:[\s]?<[^<]+?>)?)\%/U', 
    $html, 
    $matches
);

echo '<pre>';
print_r($matches);
echo '</pre>';

其中输出以下内容:

Array
(
    [0] => Array
        (
            [0] => %firstname%
            [1] => %firstname%
            [2] => %firstname%
        )

    [1] => Array
        (
            [0] => firstname
            [1] => firstname
            [2] => firstname
        )

)

只要占位符内有多个跨度,它就不起作用。我不太清楚在我的正则表达式中要调整什么。

/\%(?![0-9])((?:<[^<]+?>)?[a-zA-z0-9_-]+(?:[\s]?<[^<]+?>)?)\%/U

我将如何实现这一目标?

3 个答案:

答案 0 :(得分:1)

如果它是textContent编辑器,你可以使用解析器和WYSIWYG属性:

<?php

$html = '
    <p>%firstname%</p>
    <p>%<span>firstname</span>%</p>
    <p>%<span class="blah">firstname</span>%</p>
    <p>%<span><span>firstname</span></span>%</p>    
    <p>%<span><span><span>firstname</span></span></span>%</p>
    <p>%<span class="blah"><span>firstname</span></span>%</p>
    <div>A cool div with %firstname%</div>
    <span>And a very neat span with %firstname%</span>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

# query only root elements here
$containers = $xpath->query("/*");
foreach ($containers as $container) {
    echo $container->textContent . "\n";
}
?>

这会多次输出%firstname%,请参阅a demo on ideone.com

答案 1 :(得分:1)

试试这个正则表达式。它应该可以帮到你!

/\%(?![0-9])(?:<[^<]+?>)*([a-zA-z0-9_-]+)(?:[\s]?<\/[^<]+?>)*\%/U

答案 2 :(得分:0)

你真的需要一个正则表达式吗?你可以在这里简单地使用strip_tags()

试试这个:

echo strip_tags($html);