PHP Regex 在自定义添加的 HTML 标签之间查找文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3602251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:22:25  来源:igfitidea点击:

PHP Regex find text between custom added HTML Tags

phpregex

提问by Bathan

I have he following scenario:

我有他以下场景:

Got an HTML templatefile that will be used for mailing.

有一个HTML 模板文件,它将用于mailing.

Here is a reduced example:

这是一个简化的示例:

    <table>
<tr>
<td>Heading 1</td>
<td>heading 2</td>
</tr>
<PRODUCT_LIST>
<tr>
<td>Value 1</td>
<td>Value 2</td>
</tr>
</PRODUCT_LIST>
</table>

All I need to do is to get the HTML code inside <PRODUCT_LIST>and then repeat that code as many times as products I have on an array.

我需要做的就是获取其中的 HTML 代码<PRODUCT_LIST>,然后重复该代码与数组中的产品一样多的次数。

What would be the right PHP Regex code for getting/replacing this List?

获取/替换此列表的正确 PHP 正则表达式代码是什么?

Thanks!

谢谢!

回答by MooGoo

Assuming <PRODUCT_LIST>tags will never be nested

假设<PRODUCT_LIST>标签永远不会被嵌套

preg_match_all('/<PRODUCT_LIST>(.*?)<\/PRODUCT_LIST>/s', $html, $matches);

//HTML array in $matches[1]
print_r($matches[1]);

回答by webbiedave

Use Simple HTML DOM Parser. It's easy to understand and use .

使用简单的 HTML DOM 解析器。很容易理解和使用。

$html = str_get_html($content);
$el = $html->find('PRODUCT_LIST', 0);
$innertext = $el->innertext;

回答by shamittomar

Use this function. It will return all found values as an array.

使用此功能。它将所有找到的值作为数组返回。

<?php
function get_all_string_between($string, $start, $end)
{
    $result = array();
    $string = " ".$string;
    $offset = 0;
    while(true)
    {
        $ini = strpos($string,$start,$offset);
        if ($ini == 0)
            break;
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        $result[] = substr($string,$ini,$len);
        $offset = $ini+$len;
    }
    return $result;
}

$result = get_all_string_between($input_string, '<PRODUCT_LIST>', '</PRODUCT_LIST>');

回答by fearis

as above is ok but with performance is really horrible If You can use PHP 5 you can use DOM object like this :

如上所述是可以的,但性能真的很糟糕如果您可以使用 PHP 5,您可以像这样使用 DOM 对象:

     <?php
      function getTextBetweenTags($tag, $html, $strict=0)
    {
     /*** a new dom object ***/
    $dom = new domDocument;

    /*** load the html into the object ***/
    if($strict==1)
    {
        $dom->loadXML($html);
    }
    else
    {
        $dom->loadHTML($html);
    }

    /*** discard white space ***/
    $dom->preserveWhiteSpace = false;

    /*** the tag by its tag name ***/
    $content = $dom->getElementsByTagname($tag);

    /*** the array to return ***/
    $out = array();
    foreach ($content as $item)
    {
        /*** add node value to the out array ***/
        $out[] = $item->nodeValue;
    }
    /*** return the results ***/
    return $out;
}
?>

and after adding this function You can just use it as:

添加此功能后,您可以将其用作:

$content = getTextBetweenTags('PRODUCT_LIST', $your_html);

foreach( $content as $item )
{
    echo $item.'<br />';
}
?>

yep, i just learn this today. dont use preg for html with php5

是的,我今天才学会这个。不要在 php5 中使用 preg for html

回答by user435193

try this regular expressionin preg match all function

试试这个regular expressionpreg match all function

<PRODUCT_LIST>(.*?)<\/PRODUCT_LIST>