Parsing XML in PHP

December 7th, 2011

I’m sure many php users have needed to read some xml data into a php program and looked around hoping to find a function that can parse an XML file with a few lines of code. The road to simple XML usage may not be that short, but we’ll explore some techniques to get the data you want from an xml source.
First define some xml:

$contents = ‘
<rss version=”2.0″>
<channel>
<item>
<title>Orange Cat</title>
<link>http://www.phpprogrammer.co.uk/</link>
<description>A nice tabby.</description>
</item>
<item>
<title>Black Cat</title>
<link>http://www.phpprogrammer.co.uk/</link>
<description>A shy cat.</description>
</item>
</channel>
</rss>
‘;

Then parse the XML with this PHP code:

$parser = xml_parser_create('UTF-8');
$did_parse = xml_parse_into_struct($parser, trim($contents), $xml_values);
if(!$did_parse) {
	echo xml_error_string(xml_get_error_code($parser)) . " error on line: " . xml_get_current_line_number($parser);
}

The above example takes an “XML document”, in the form of a string, and parses it into an array ($xml_values). The array style and structure is complicated, but we will go through some simple steps to rearrange it into a usable format. $parser is an object. Functions like xml_parse_into_struct($parser … ) take $parser as an argument.
Read the rest of this entry »

Simplifying PHP string reading with sscanf()

December 6th, 2011

In this article we’ll see how to use sscanf() to read or parse a string in a customized way. You may have heard of the scan series of functions before, especially in other types of programming. In c based console applications that could read user input and then print text results, scan was the opposite of print. That only has a little bit to do with the function we will be talking about here.

sscanf() isn’t something you will use every day. You could try to pick apart strings with other standard string functions, or use variations on preg_match() or preg_replace(). Regardless there are situations where any of these methods might be best. The first example of sscanf will be pretty simple;

$scanned_float = 0.0;
sscanf("9.64", "%f", $scanned_float);
echo $scanned_float;

You may have guessed that $scanned_float now equals 9.64. The first parameter is the string to be read. The second is a format string based on the format used by the printf() function. Any additional arguments will be filled with values computed from the function, in order. In this case the format means 1 float value. There is one variable to receive it, $scanned_float.

$scanned_float = 0.0;
sscanf("height: 9.64", "%f", $scanned_float);
echo $scanned_float;

Read the rest of this entry »

HTML Character Codes in PHP

December 6th, 2011

PHP is a language that is highly associated with outputting HTML so that the output is shown properly in a browser. If you send the browser text with no context and no concerns for whether it is in HTML format the results are problematic and hard to predict. To format text into HTML there are many conversions that may be needed. A simple example would be converting newlines to <br /> tags. There is a well know PHP function for that of course, nl2br(). If your text uses < somewhere it also needs to be converted since HTML browsers would think it is the start of a tag. &lt; is the correct representation of < in HTML.

echo htmlspecialchars('In "real" math, 5 < 5.1');

Read the rest of this entry »

String Comparison in PHP – When are strings equal

December 6th, 2011

It’s simple enough to check if strings are exactly equal in PHP, just use ==, the standard comparison operator. However, this assumes we are only using strings or string type variables. It is a case sensitive comparison only. In this short article we are going to go though some alternate string comparison options and learn what they are good for.

First is exact type comparison, ===

$str1 = "100DollarsCash";
if($str1 == 100) {
	echo "it equals 100. ";
}
if($str1 === 100) {
	echo "it is 100. ";
}

Read the rest of this entry »

PHP Sessions – Let’s Start from the Beginning

December 6th, 2011

You don’t need to know about cookies (though we have an article on cookies here) to get through this article or to use sessions. Just keep in mind that they usually automatically use and set cookies, and like cookies you have to do certain things before the first output is sent (including doctype, <html>, or any whitspace).

The main thing you need to do is start a session before output is started. Also, for those familiar with it, output buffering can help give you more flexibility with where session_start() or setcookie() are used.

session_start();

Read the rest of this entry »

Cookies – PHP can help with cookies

December 6th, 2011

Cookies are a long standing method of storing information so it can be used again on a per user basis. Prior to database and server based sessions becoming popular cookies saw use in more complex ways. There are still a component of sessions and used on most web sites, from shopping carts to web mail. In PHP it isn’t hard to get started with cookies.

setcookie("UserInfo", "3644 Alder St", time() + 3600, "/~phppro/", "iodigitalsec.com");

That example shows an example of setting the cookie UserInfo to a street address.
Read the rest of this entry »