我的字符串如
<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('/:( *<)/', " < ", $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
将<
转换为<
。
REGEX: /<(?=[ds])/
1。
<(?=[ds])
匹配<
和space
或digit
的阳性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])/', " < ", $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('/[<](?=[^>]+[<])/', "<", $string);
它寻找&lt;其次是A&lt;而不是>
因此,它忽略了&lt;是标签的一部分。