1

I'm desperately trying to access content in a nested div :

<tr>
<th class="monthCellContent" style="vertical-align : top">
    <div class="monthEventWrapper">
        <div class="monthEvent">
            <a class="event"
                href="/event/1"
                title="test title updated - test place - 09:00-10:00">
                    09:00
                    <span class="showForMediumInline">
                        test title updated test place
                    </span>
            </a>
        </div>
    </div>
</th>
</tr>

I'm trying to access "09:00" and "test title updated test place" in the link.

I'm somehow stuck at

<div class="monthEventWrapper">

which I can access with

$items = $crawler->filter('div[class="monthEventWrapper"]');
print "\n found " . count($items) . " monthEventWrapper divs\n";

found 35 monthEventWrapper divs

but I cannot access

<div class="monthEvent">

with

$items = $crawler->filter('div[class="monthEvent"]');
print "\n found " . count($items) . " monthEvent divs\n";

found 0 monthEvent divs

I tried all variations around

foreach ($items as $item) {
    foreach ($item->childNodes as $child) {
        $value .= $paragraph->ownerDocument->saveHTML($child);
    }
}

and

$crawler->filterXPath('//div[@class="monthEvent"]')

with no luck.

The html passes validations and there's no js.

Thanks !

frian
  • 46
  • 1
  • 5

1 Answers1

0

This a workaround type of code:

<?php

use Symfony\Component\DomCrawler\Crawler;
require_once(__DIR__ . '/../vendor/autoload.php');

$html = <<<'HTML'
<!DOCTYPE html>

<html>
    <body>
        <tr>
            <th class="monthCellContent" style="vertical-align : top">
                <div class="monthEventWrapper">
                    <div class="monthEvent">
                        <a class="event"
                            href="/event/1"
                            title="test title updated - test place - 09:00-10:00">
                                09:00
                                <span class="showForMediumInline">
                                    test title updated test place
                                </span>
                        </a>
                    </div>
                </div>
            </th>
        </tr>
    </body>
</html>

HTML;

$crawler = new Crawler($html);
$crawlerFiltered = $crawler->filter('div[class="monthEventWrapper"] a');

$results = [];
$childResults = [];
for ($i=0; $i<count($crawlerFiltered); $i++) {
    $results[] = removeLeadingAndTrailingWhiteCharsAndNewLine($crawlerFiltered->eq($i)->text());

    $children = $crawlerFiltered->eq($i)->children();
    if (count($children)) {
        for ($j=0; $j<count($children); $j++) {
            $childResults[] = removeLeadingAndTrailingWhiteCharsAndNewLine($children->eq($j)->text());
        }
    }
}

$results[0] = substractSpan($results[0], $childResults[0]);

function removeLeadingAndTrailingWhiteCharsAndNewLine(string $text) : string
{
    $pattern = '/(?:\r\n[\s]+|\n[\s]+)/s';
    return preg_replace($pattern, '', $text);
}

function substractSpan($text, $textToSubstract) : string
{
    $length = strlen($text) - strlen($textToSubstract);
    return substr($text, 0, $length);
}

echo 'Parent Nodes:' . PHP_EOL;
var_export($results);
echo PHP_EOL;
echo 'Child Nodes:' . PHP_EOL;
var_export($childResults);

echo PHP_EOL;
echo 'Time: '; 
echo $results[0];

echo PHP_EOL;
echo 'Text: ';
echo $childResults[0];

but gives this result:

Parent Nodes:
array (
  0 => '09:00',
)
Child Nodes:
array (
  0 => 'test title updated test place',
)
Time: 09:00
Text: test title updated test placee

Note I used for loop with ->eq(<node-number>) that gives Crawler instance instead of DOMNode that you get by using foreach

Note the code assumes the wanted text part 9:00 is at the beginning.

Jimmix
  • 5,644
  • 6
  • 44
  • 71