Jump to content

Need Help Removing Certain Elements From Array


savagenoob

Recommended Posts

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"]'); 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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>"; 
} 

Link to comment
Share on other sites

. . . 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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.