正则表达式,用于将 < 中的 替换为<TR><TD>某些特殊字符



我的字符串如

<td align='left'style='font-family: Courier New;'>
	Therapeutic target for gout patients: <6.0 mg/dL
</td>
<td class='tableCell'>
	< OR = 30
</td>

听到我的代码

$string  = preg_replace('/:( *<)/', " &lt; ", $string);
$DOM = new DOMDocument();
libxml_use_internal_errors(true); // Hide warnings
$DOM->loadHTML($string);
$rows = $DOM->getElementsByTagName('tr');
for ($i = 0; $i < $rows->length; $i++) 
{
    echo "<HR/>";
    $cols = $rows->item($i)->getElementsbyTagName('td');
    for ($j = 0; $j < $cols->length; $j++) 
    {
        echo "<br/>".$cols->item($j)->nodeValue;
    }
}

First&lt; td>显示"痛风患者的治疗靶标&lt; 6.0 mg/dl",其次显示空白。任何人都可以告诉我一些表达式,可以帮助我获取两个数据。

预先感谢..!

在这里我们使用DOMDocument提取所需的数据,在这里我们使用regex<转换为&lt;

REGEX: /<(?=[ds])/

1。 <(?=[ds])匹配<spacedigit的阳性lookahead

尝试此代码段

<?php
ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$string='<td align="left" style="font-family: Courier New;">
    Therapeutic target for gout patients: <6.0 mg/dL
</td> 
<td class="tableCell">
    < OR = 30
</td>';
$string  = preg_replace('/<(?=[ds])/', " &lt; ", $string);
$domDocument= new DOMDocument();
$domDocument->loadHTML($string);
$domDocument->encoding="utf-8";
foreach($domDocument->getElementsByTagName("td") as $value)
{
    echo $value->textContent;
    echo PHP_EOL;
}

输出:

Therapeutic target for gout patients: < 6.0 mg/dL < OR = 30

使用比使用的sahil gulati更残酷的言论。

$string  = preg_replace('/[<](?=[^>]+[<])/', "&lt;", $string);

它寻找&lt;其次是A&lt;而不是>
因此,它忽略了&lt;是标签的一部分。

最新更新