0

I have this code

$myurl = file_get_contents('https://myurl.html');
$doc = new DOMDocument();
@$doc->loadHTML($myurl);
$divs = $doc->getElementsByTagName('div');

foreach($divs as $div) {
    if ($div->getAttribute('id') === 'j-product-desc') {
        echo "<br/>".$div->nodeValue;
    }
}

Result: It is displaying result something like this with no break line.

Item specifics Item Type: Jewelry Packaging & Display Material: Acrylic Jewelry Packaging & Display Type: Cases & Displays Item Length: 16.5 cm

Required Result I want result like this with break after every specification.

Item specifics

Item Type: Jewelry Packaging & Display

Material: Acrylic Jewelry Packaging & Display

Type: Cases & Displays

Item Length: 16.5 cm

3
  • Is the entire string in a single nodeValue? Commented Oct 30, 2016 at 11:04
  • yes entire string in a single nodeValue Commented Oct 30, 2016 at 11:08
  • Then you're going to have difficulty. You'll need to work out what the rules are for where the each part of the attribute begins and ends and split the string accordingly and then output with HTML <br /> tags. What would make things easier is if there are newlines already in the text. If you view-source in your browser, are they on different lines? Commented Oct 30, 2016 at 11:11

1 Answer 1

1

Try this:

$myurl = file_get_contents('https://myurl.html');
$doc = new DOMDocument();
@$doc->loadHTML($myurl);
$divs = $doc->getElementsByTagName('div');

foreach($divs as $div) {
    if ($div->getAttribute('id') === 'j-product-desc') {
        $desc = str_replace("\n", '<br>', $div->nodeValue);
        echo "<br/>".$desc;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.