Parsing With PHP [Part1]

PHP July 28th, 2008

Hehe.. I learn how to parsing with PHP. I read some books and find so many tricks to parsing text using PHP.. So I’ll try one by one^^ First, I tried to simple parsing with PHP. There are some technique for parsing that I learned today.

Parsing Comma-Separated List

How to parsing from comma-separated list then make it to array. First you can using explode() function. Lookout this example.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
// define comma-separated list
$menu = "butter, milk, sugar, salt, flour, caramel";
// decompose string into array
// using comma as delimiter
$menu = explode(", ", $menu);
// iterate over array
// print individual elements
foreach ($menu as $i) {
echo $i . "<br />";
}
?>

PHP’s explode() function makes it a single-step process to split a comma-separated string list into an array of individual list elements. The previous listing clearly illustrates this: the explode() function scans the string for the delimiter and cuts out the pieces around it, placing them in an array. Once the list items have been extracted, a foreach() loop is a good way to process the resulting array.

The second one you can use split() function. Lookout this example.

1
2
3
4
5
6
7
8
9
10
11
12
<?
// define comma-separated list
$menu = "butter, milk, sugar, salt, flour, caramel";
// decompose string into array
// using comma as delimiter
$menu=split(", ",$menu);
// iterate over array
// print individual elements
foreach ($menu as $i) {
echo $i . "<br />";
}
?>

Like explode() function, PHP’s split() function makes it a single-step process to split a comma-separated string list into an array of individual list elements. Choose one of them to parsing comma-separated list.

Preview

Parsing URL’s

How to extract the protocol, domain name, path, or other significant component of a URL. You can using parse_url() function. In this case I’ll try to extract the protocol, domain name, path, or other significant component of “http://z9.invisionfree.com/bww2/index.php?showtopic=57466″. Lookout this example.

1
2
3
4
5
6
7
8
9
10
<?php
// define URL
$url = "http://z9.invisionfree.com/bww2/index.php?showtopic=57466";
// parse URL into associative array
$data = parse_url($url);
// print URL components
foreach ($data as $k=>$v) {
echo "$k: $v <br />";
}
?>

The parse_url() function is one of PHP’s more useful URL manipulation functions. Pass it a Uniform Resource Locator (URL), and parse_url() will go to work splitting it into its individual components. The resulting associative array contains separate keys for the protocol, host name, port number, remote path, and GET arguments. You can then easily access and use these keys for further processing—for example, the variable $data['host'] will return the value www.melonfire.com.

Preview

Hehe coding is fun^^

Reference:
-PHP manual
-Vaswani Vikram, 2007, “PHP Programming Solution”, McGraw-Hill

Sphere: Related Content

Tags: , ,
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...






Related Post:


Leave a Comment

Blogroll Link Update