php - получить текст без класса или тегов html dom parser
Я застрял в типичном случае... Мне нужен какой-то текст из таблицы, который не имеет ни класса, ни тега... это просто текст... Я хочу получить только этот текст. Мне нужно очистить Я ХОЧУ ЭТОТ ТЕКСТ. Как я это сделаю
МОЙ HTML
<td class="example">
<strong>text in strong</strong><br>
<strong>2nd text in strong:</strong>
I WANT THIS TEXT
<br>
<strong><span style="color:red;">another text</span></strong>
<br>
<a href="#" target="_blank">Click Here</a>
</td>
До сих пор я пытался: так как мы должны очистить несколько строк, я использую цикл foreach
foreach($html->find('td.example') as $element){
echo $element->find('strong', 1)->outertext . "<br/>";
}
1 ответ
Если мы предположим, что ваша html-строка находится в переменной $html, то должно работать следующее регулярное выражение:
/** Replace the carriage return with '^' */
$html = str_replace("\r", "^", $html);
/** Replace the line feed with '~' */
$html = str_replace("\n", "~", $html);
/** regular expression is used to match the text */
preg_match("/<strong>.*<\/strong><br>.*<strong>.*<\/strong>(.+)<br><strong><span style="color:red;">.*<\/span><\/strong>/iU", $html, $matches);
/** The '^' is replaced with '\r' */
$matches[1] = str_replace("^", '\r', $matches[1]);
/** The '~' is replaced with '\n' */
$text = str_replace("~", '\n', $matches[1]);
Переменная $text содержит текст совпадений