I've been lurking Stackoverflow for a possible answer for hours and although I found some solutions, none worked in my case.
I need to get the text of the div and run it thru a foreach
loop to eventually create a new database record for each div content.
Everything works until I face divs with multi-line content and <br>
tags.
I have tried:
$quotes = $finder->query("//*[contains(@class, normalize-space('$quote'))]//text()");
But it doesn't seem like normalize-space()
has any effect because instead of pushing the whole text into one array it creates a new array after every <br>
instead.
More code:
$quotes = $finder->query("//*[contains(@class, normalize-space('$quote'))]//text()");
$authors = $finder->query("//*[starts-with(@class,'$author')]/child::a");
foreach ($quotes as $key => $quote) {
{
$quote = trim($quote->textContent);
$dataArr[] = $quote;
$authorName = preg_split("/[\s,-,@]+/", $authors[$key]->textContent);
if (count($authorName) < 5) {
$authorName = $authorName[1];
} else if (count($authorName) > 5) {
$authorName = $authorName[1] . ' ' . $authorName[2] . ' ' . $authorName[3];
} else if (count($authorName) > 6) {
$authorName = $authorName[1] . ' ' . $authorName[2] . ' ' . $authorName[3] . ' ' . $authorName[4];
} else {
$authorName = $authorName[1] . ' ' . $authorName[2];
}
array_push($dataArr, $authorName);
}
HTML structure that is extracted correctly:
<div class="b-list-quote2__item "><a href="/" class="b-list-quote2__item-text js-quote-text">
A random quote here...
</a><div class="b-list-quote2__item-category">
<a href="/quotes/albert-einshtein?q=17856">Albert Einstein</a>
In this case, I get an Array with the Quote and Author that I later chunk by 2 and use in other functions
[0] => A random quote here...
[1] => Albert Einstein
HTML structure I'm having the problem with:
<div class="b-list-quote2__item "><a href="/" class="b-list-quote2__item-text js-quote-text" style="position: relative; max-height: none;">
Quote line 0,
<br>Quote line 1,
<br>Quote line 2,
<br>Quote line 3,
</a><div class="b-list-quote2__item-category">
<a href="/quotes/karmelita-kruglaia?q=249176">Tesla</a>
In this case, a new array item is added for every line of text thus something like
[0] => Quote line 0
[1] => Quote line 1
[2] => Quote line 2
[3] => Quote line 3
With no "author" in the array which in this case should be "Tesla".
How a good array should look:
[0] => Quote line 0 Quote line 1 Quote line 2 Quote line 3
[1] => Tesla