Test the presence and value of an xml tag and replace a variable with the result if not empty

HI,
Struggling with the code below:
In the xml it reads in, an item may/may not have a 'CELL' tag and that tag may/may not have a value (whatever the phone number for the cell is).

So some items may have:
<item>
<phone_home>5551212</phone_home>
<cell>5559999</cell>
</item>

others may have:

<item>
<phone_home>5551212</phone_home>
<cell></cell>
</item>

and others may have simply:

<item>
<phone>5551212</phone_home>
</item>

I'm trying to test the value of the cell phone tag if present and if so, make it the 'replacement value' for the $phone variable in my code below. In simpler terms: I want to phone a customer if they have a cell on file instead of their 'home phone' number. If no cell phone number available: stick with their home phone.

Hope that makes some sense.
thanks in advance.
todd
// ITERATE OVER THE OBJECT TO SET THE DB AND SEND THE EMAILS
foreach ($obj as $item)
{
    
// GET THE VARIABLES WE NEED
    $email = trim((string)$item->EMAIL_ADDRESS_DV);
    $complete = trim((string)$item->INV_TIME);
    $promise = trim((string)$item->PROM_TIME);
    $license = trim((string)$item->LICENSE);
    $name = trim((string)$item->FIRST_NAME);
    $phone = trim((string)$item->HOME_PHONE);
    $cell = trim((string)$item->PHONE_CELL);
    
*THIS IS THE PROBLEM POINT: NEED TO CHECK IF THE CELL NUMBER EVEN EXISTS (the tag may not even be present in the xml, or if the tag is there and the number is blank, then ignore. Otherwise, make the number for $cell, the new $phone number and continue with below)

	$phone = preg_replace("/[^0-9]/","",$phone);
		if (substr($phone,0,1) == '1'){
		$phone = substr($phone,1);}
		if (strlen($phone) == 7) $phone = '902' . $phone;
	$advisor = trim((string)$item->ADV_NAME);
    $datex    = date('Y-m-d'); // ISO8601 DATE STRING
    $datec    = date('c');     // ISO8601 DATETIME STRING

Open in new window

LVL 1
tjyoungAsked:
Who is Participating?
 
blueghoztConnect With a Mentor Commented:
quick fix for you below - replaced your comment with a test for a value in $cell - I am not sure about the subsequent rules you have for prepending 902 (I am UK based) and if this applies to cell numbers so you might want to put that whole testing for type of phone number into the if $cell exists condition.
// ITERATE OVER THE OBJECT TO SET THE DB AND SEND THE EMAILS
foreach ($obj as $item)
{
    
// GET THE VARIABLES WE NEED
    $email = trim((string)$item->EMAIL_ADDRESS_DV);
    $complete = trim((string)$item->INV_TIME);
    $promise = trim((string)$item->PROM_TIME);
    $license = trim((string)$item->LICENSE);
    $name = trim((string)$item->FIRST_NAME);
    $phone = trim((string)$item->HOME_PHONE);
    $cell = trim((string)$item->PHONE_CELL);
    
    if(strlen($cell) > 0) $phone = $cell;

        $phone = preg_replace("/[^0-9]/","",$phone);
                if (substr($phone,0,1) == '1'){
                $phone = substr($phone,1);}
                if (strlen($phone) == 7) $phone = '902' . $phone;
        $advisor = trim((string)$item->ADV_NAME);
    $datex    = date('Y-m-d'); // ISO8601 DATE STRING
    $datec    = date('c');     // ISO8601 DATETIME STRING

Open in new window

0
 
tjyoungAuthor Commented:
Can't wait to try it. I'm on my way home from work but will give it a go when I get home.
the 902 applies to cell phones as well.
check back with you shortly.
Many thanks
0
All Courses

From novice to tech pro — start learning today.