Angel Carlson
Angel Carlson

Reputation: 47

altering text returned from source

This code pulls the name of a shipping option from Canada Post's server. I need to somehow specify that if it comes back as "Expedited" it should say "Canada Post" instead. Is that possible?

$name = substr(
   $resultXML,
   strpos($resultXML, "<name>") + strlen("<name>"),
   strpos($resultXML, "</name>") - strlen("<name>") - strpos($resultXML, "<name>")
);

Upvotes: 0

Views: 37

Answers (2)

arkoak
arkoak

Reputation: 2497

You need to use simple xml parser which is built into php

// make it more error friendly
$saved = libxml_use_internal_errors(true);

//load the xml as object
$xml = simplexml_load_string($inputString);
if ($xml === false) { 
    foreach (libxml_get_errors() as $error) {
        echo $error->message, "\n";
    }
    libxml_use_internal_errors($saved);
    return;
}

libxml_use_internal_errors($saved);

echo $xml->asXML(); //you can see the XML here

get the name as per object position like $xml->obj1->name etc

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 73041

You can use a simple if statement:

if ($name === 'Expedited') {
    $name = 'Canada Post';
}

Upvotes: 1

Related Questions