PHP Trics
preg_split
When you want to explode a message by spaces, you may want to use the preg_split function, because sometimes an enduser accidently sends two spaces. For example, he sends DEPERS 1623HV. Your script expects a ZipCode at the first position, but because of the spaces it will be an other position. This preg_split solves this issue.
$lMessageParts = preg_split("/[\s]+/", $aObjMoMessage->MESSAGE);
If you want to split the message for words only, then you can use this code:
$lMessageParts = preg_split("/[^A-Za-z]+/", $lMbiMoMessage->MESSAGE);
This method makes sure that when the user sends "IAMOR: PAUL & NICOLE", the output will still be Array("IAMOR", "PAUL", "NICOLE"), which is perfect if you want to process the words only. This preg is being used in the iTV games.
SimpleXML attributes
To fetch the attribute of an element, try this:
$objXml->element->attributes()->name;
<?xml version="1.0" encoding="UTF-8"?>
<test>
<element name="showcase">true</element>
</test>
To print out showcase here, echo $objXml->element->attributes()->name;
<?xml version="1.0" encoding="UTF-8"?>
<test>
<element name=\"showcase\">true</element>
<other>
<jadi test=\"true\">okay then!</jadi>
</other>
</test>
To print out "true" from the jadi element, use echo $objXml->other->jadi->attributes()->test;
CDATA with dom document
To create a CDATA element when using DOM Document in PHP, you can add the CDATA section right after the appendchild. Example:
$objXml = new DOMDocument("1.0", "UTF-8");
$lRequests = $objXml->appendChild(new DOMElement("Requests"));
$lRequests->appendChild(new DOMElement("foo"))->appendChild($objXml->createCDATASection("bar"));
echo $objXml->saveXML();
Outputs:
<?xml version="1.0" encoding="UTF-8"?>
<Requests><foo><![CDATA[bar]]></foo></Requests>