Link to home
Start Free TrialLog in
Avatar of tjyoung
tjyoung

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of blueghozt
blueghozt
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tjyoung
tjyoung

ASKER

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