Parsing and encoding directions

This is continued from the previous post. We now wanted to parse the array of directions to extract the steps. Another big challenge was to fit the large amount of data into the QR code. We used the PHP GD library to maps the occurrence of the words in a bar chart and encode them .The following code does exactly this.

[php]
<pre>//:::::::::::::::: Retrieves the useful stuff from the json file ::::::::::::::::
$jsonIterator = new RecursiveIteratorIterator(    new RecursiveArrayIterator(json_decode($data, TRUE)), decide the     RecursiveIteratorIterator::SELF_FIRST);
$counter=0;
foreach ($jsonIterator as $key => $val)
{
if(!is_array($val)) {
if($key == "html_instructions")
{
$directions[$counter] = array("step" => $val);
//print_r($directions);
$output .= $val;
$output .= ".";
$counter++;
}
}
}
//::::::::::: Reduce the content with 1 to 1 linkage ::::::::::::::::
$common=array(‘right’,’left’,’Turn’,’Pass’,’keep’,’Continue’,’the’,’onto’);
$encode=array(‘$’,’_’,’!’,’#’,’@’,’~’,’?’,’`’);
$i=0;
while(isset($common[$i]))
{
$output=str_replace($common[$i],$encode[$i],$output);
$i++;
}
$output=strip_tags($output);
//echo $output."<br/><br/>";
//$response = $_GET["callback"] . "(" . json_encode($directions) . ")";
[/php]

Lets briefly understand what are we doing.
We are using The RecursiveIterator interface of php to iterate over the array and extracts the steps. We assume a key:value pair and check if the key is html_instructions then append it to the $output string.
After that we are simply replacing the words in the array $common with the equivalent symbols in array $encode

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s