need help with changing returned text

This code pull 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, “”) + strlen(""), strpos($resultXML, “”) - strlen("") - strpos($resultXML, “”));

Yes it’s possible. How do you think you would check to see if a variable equaled something? How you would you reassign a different value to it, if it did?

[php]<?php
$name = ‘The Package is Expedited’; // Expedited needs to be replaced.

echo $name . “
\n”;
/* Pattern(s) /
$patterns[0] = ‘Expedited’;
/
Replacement(s) */
$replacements[0] = ‘Canada Post’;

$result = str_ireplace($patterns, $replacements, $name);

echo $result;[/php]

Of course you probably want to see if it’s a Canadian Package or not:

Roughly something like the following?
[php]<?php
$name = ‘The Package is Expedited’; // Expedited needs to be replaced.

//function to validate postal code of canada
function validateCanadaZip($zip_code, $name)
{

/* Pattern(s) /
$patterns[0] = ‘Expedited’;
/
Replacement(s) /
$replacements[0] = ‘Canada Post’;
/
Check to see if it’s a Canadian Parcel */
if(preg_match("/^([a-ceghj-npr-tv-z]){1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}$/i",$zip_code))
{
return str_ireplace($patterns, $replacements, $name); // Change it if it is:
}
else
{
return $name; // Do nothing if it ain’t:
}
}

echo 'Unchange string : ’ . $name . “
\n”;

$result = validateCanadaZip(‘48150’, $name);

echo 'Trying it with a U.S. Zip Code ’ . $result . “
\n”;

$result = validateCanadaZip(‘A1B2c5’, $name);

echo 'Trying it with a Canadian Zip Code '. $result . “
\n”;
[/php]

It works, but of course not thoroughly tested and I am sure changes will have to made to fit your script(s).

Sponsor our Newsletter | Privacy Policy | Terms of Service