Jump to content

Help with syntax on statement? parsing woes


CodeMama

Recommended Posts

I'm trying to take a paragraph then break it into linebreaks and grab each line separately but it's not working, I can get the paragraph but my lines are not I get the object id #6 or #7 everytime

Here's my code

// grab all the paragraphs on the page
$xpath = new DOMXPath($dom);
//$graphs = $xpath->evaluate("/html/body//p");
$graphs=$dom->getElementsByTagName("p");
$lines=$dom->getElementsByTagName("br");
// Set $i =5 because first 5 paragraphs are not inspections
for ($i = 5; $i+1 < $graphs->length; $i++) {
    $paragraph = $graphs->item($i);
    
    
    $text = $dom->saveXML($paragraph);
    $text = trim($text);
    
    /*
        //my experiment for getting line br
        for ($b = 1; $b+1 < $lines->length; $b++) {*/
    $title = $lines->item($i);
    $addie= $lines->item($i);
     

for one you are missing a close bracket for your loop:

<?php
// grab all the paragraphs on the page
$xpath = new DOMXPath($dom);
//$graphs = $xpath->evaluate("/html/body//p");
$graphs=$dom->getElementsByTagName("p");
$lines=$dom->getElementsByTagName("br");
// Set $i =5 because first 5 paragraphs are not inspections
for ($i = 5; $i+1 < $graphs->length; $i++) {
    $paragraph = $graphs->item($i);
   
   
    $text = $dom->saveXML($paragraph);
    $text = trim($text);
   
    /*
        //my experiment for getting line br
        for ($b = 1; $b+1 < $lines->length; $b++) {*/
    $title = $lines->item($i);
    $addie= $lines->item($i);
}

    ?>

Yeah maybe but that isn't the problem I probably just snipped it when I cut and pasted my code.  A missing bracket would give me the blank page of horror but that isn't what I'm getting I'm getting a return of the $graph but then the $line var isn't working.

tidied up the PHP DOM code for ya ;D

 

also i have a feeling the problem lays where you define $dom. can you paste that part? because it is not defined there. ???

<?php
// grab all the paragraphs on the page
$xpath = new DOMXPath($dom);
//$graphs = $xpath->evaluate("/html/body//p");
$graphs=$dom->get_elements_by_tagname("p");
$lines=$dom->get_elements_by_tagname("br");
// Set $i =5 because first 5 paragraphs are not inspections
for ($i = 5; $i+1 < $graphs->length; $i++) {
    $paragraph = $graphs ->item($i);
   
   
    $text = $dom->saveXML($paragraph);
    $text = trim($text);
   
    /*
        //my experiment for getting line br
        for ($b = 1; $b+1 < $lines->length; $b++) {*/
    $title = $lines->item($i);
    $addie= $lines->item($i);
}?>
    

Gives me no errors just a blank page:

 

<?php

//create a new DOM Document
$dom = new domDocument;
// grab all the paragraphs on the page
$xpath = new DOMXPath($dom);
//$graphs = $xpath->evaluate("/html/body//p");
$graphs=$dom->getElementsByTagName('p');
$lines=$dom->getElementsByTagName('br');
// Set $i =5 because first 5 paragraphs are not inspections
for ($i = 5; $i+1 < $graphs->length; $i++) {
    $paragraph = $graphs ->item($i);
   
   
    $text = $dom->saveXML($paragraph);
    $text = trim($text);
   
    /*
        //my experiment for getting line br
        for ($b = 1; $b+1 < $lines->length; $b++) {*/
    $title = $lines->item($i);
    $addie= $lines->item($i);
}?>
     

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.