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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blueghoztCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.