savagenoob Posted October 8, 2011 Share Posted October 8, 2011 I used DOM to parse data from html, then I made the data go into an array. the array now looks like Array([0]->street[1]->city, st, zip[2]->phone[3]->fax) and it is quite a long array. The problem is that when there is no fax number on the html, it doesnt show the fax on the array, so when I am trying to loop through this in a table it is throwing off my cells. Is there a way to delete all entries in an array that include Fax:* ? Is there a way to exclude all nodes that have Fax* using DOM? My current DOM looks like... $address = $xpath->query('//td[@id="addressText"]'); Quote Link to comment Share on other sites More sharing options...
savagenoob Posted October 9, 2011 Author Share Posted October 9, 2011 Yes no maybe so? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 9, 2011 Share Posted October 9, 2011 It would be more efficient to change your processing code to simply skip the fax elements. But, yes, you can remove the fax elements first. It would be helpful to see the relevant code you have now to see what would be the best way to implement though. Quote Link to comment Share on other sites More sharing options...
savagenoob Posted October 9, 2011 Author Share Posted October 9, 2011 Well I posted the xpath code I used to get the data from the <td> elements with id addressText, the address, phone number, and fax number reside in these tags. Quote Link to comment Share on other sites More sharing options...
savagenoob Posted October 14, 2011 Author Share Posted October 14, 2011 Still havent figured out how to remove all "Fax:xxxxxxx" entries from my array or modify my xpath code to skip all <td> elements that contain "Fax:xxxxxxx". Any suggestions? Here is the entire code: $ $dom = new domDocument; /*** load the html into the object ***/ $dom->loadHTML($html); /*** discard white space ***/ $dom->preserveWhiteSpace = false; $xpath = new DOMXPath($dom); $tags = $xpath->query('//div[@id="large_green"]'); $address = $xpath->query('//td[@id="addressText"]'); $result = array(); $agency = array(); foreach ($address as $tag) { $result[] = trim($tag->nodeValue) . "<br>"; } foreach ($tags as $tag) { $agency[] = trim($tag->nodeValue) . "<br>"; } array_shift($agency); for ($i = 0, $e = 0; $i < count($result); $i += 4, ++$e) { $comagency = strip_tags($agency[$e]); $comaddress = strip_tags($result[$i]) . " " . strip_tags($result[$i+1]); $comphone = strip_tags($result[$i+2]); ?> <tr> <td><?php echo $comagency;?></td><td><?php echo $comaddress;?></td><td><?php echo $comphone;?></td><td></td><td align="center"><input type="checkbox" name="agencyadd[]" value="<?php echo $comagency . " Address:" . $comaddress;?>"/></td> </tr> <?php } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 14, 2011 Share Posted October 14, 2011 Well, now that you have posted the code I can help you. It *looks* like you are referencing data by it's position in the results returned. That's kind of a poor implementation and can easily break depending on how the input changes. This logic should work, but you might need to modify the actual text being compared. I can't be sure since I can't see the input data. foreach ($address as $tag) { //If $tag->nodeValue contains the string "Fax:", skip this entry if(strpos($tag->nodeValue, "Fax:")===false) { continue; } $result[] = trim($tag->nodeValue) . "<br>"; } Quote Link to comment Share on other sites More sharing options...
savagenoob Posted October 14, 2011 Author Share Posted October 14, 2011 Hm, can I put a wildcard after Fax: ? It will list the fax number after Fax: and I believe this is explicitly looking for "Fax:" not "Fax: (111) 111-1111" Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 14, 2011 Share Posted October 14, 2011 . . . I believe this is explicitly looking for "Fax:" not "Fax: (111) 111-1111" You believe wrong. If I was doing a check for a value exactly matching "Fax:" I would have done something like if($tag->nodeValue == "Fax:") { continue; } But, I used the function strpos() which Returns the numeric position of the first occurrence of needle in the haystack string. http://us.php.net/manual/en/function.strpos.php Quote Link to comment Share on other sites More sharing options...
savagenoob Posted October 14, 2011 Author Share Posted October 14, 2011 Oh sweet, thanks bud. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.