Archive

Posts Tagged ‘convert’

Something To Be Aware Of JavaScript isNaN

March 17th, 2009 Tech 1 comment

The isNaN() function (NaN stands for Not a Number) can be useful if you are looking at form inputs or similar and is used to detect if a value is not a number. For example, the following code shows the output of isNaN() on two variables.

var number42 = "42";
var wibble = "wibble";
alert(isNaN(number42)); // Prints out false
alert(isNaN(wibble)); // Prints out true

This first test is true because the number 42 is, in fact, a number. The second test is false because wibble isn’t a number. This is simple enough, but what if we started looking at some different values?

var empty = "";
var nothing = null;
alert(isNaN(empty)); // Prints out false
alert(isNaN(nothing)); // prints out false

What is happening here is that isNaN() tries to converts any value it is given into an number, which means that "" and null get converted to 0 and the function returns false because they are now numbers. You can try this out yourself by doing one of the following:

var empty = "";
alert(+empty); // Prints out 0
alert(Number(empty)); // Prints out 0

To solve this problem you can use the parseInt() function. This takes a string as an input and tries to convert it into a number, if this is not possible then it returns NaN. If parseInt() is given an empty string or a null value it will also return NaN, rather than converting them to 0. This gives us a mechanism by which we can test values using a combination of parseInt() and isNaN().

var empty = "";
alert(isNaN(parseInt(empty))); // Prints out true

Finally, if we give isNaN() and parseInt() an undefined value the results are as follows.

var nothing;
alert(parseInt(nothing) + ", " + isNaN(nothing)); // Prints out NaN, true

The isNaN() function returns true because it can’t convert the value into any number.

var nothing;
alert(+nothing);
alert(Number(nothing));

This code prints out NaN for both of the alert statements.

Correcting Wrong Character Encoding In MySQL

March 16th, 2009 Tech 1 comment

Sometimes, especially when moving data from one server to another, you might find that you have encoded your MySQL database incorrectly. This problem with first show itself if you have the database encoded in one charset and your website set to display in another. If this is the case then you will find strange characters appearing in your text, especially when using punctuation marks. If you are unable or unwilling to change the character encoding on the site then you need to change how the data is encoded in the database.

The most common sort of thing you might want to do is change from iso-8859-1 (or windows-1252) to UTF-8. This can be done in one of two ways.

The first way is to simply alter the table so that the column contains a different charset.

ALTER TABLE table MODIFY col1 VARCHAR(50) CHARACTER SET 'utf8';

However, if your database has already been set up and your data has already been inserted in the wrong format then you can also update the data in the column using the CONVERT command. The following snippet turns our latin1 data into uncoded binary data and then into utf8.

UPDATE table SET col1=CONVERT(CONVERT(CONVERT(col1 USING 'latin1') USING BINARY) USING 'utf8');

You should also make sure that the connection to the database is done through a specific character set. This is done by using the SET NAMES command and the SET CHARACTER SET.

SET NAMES 'charset_name'
SET CHARACTER SET 'charset_name';

These two commands basically set some values in your MySQL database, for more information on what is set look at the Connection Character Sets and Collations page on the MySQL website. This ensures that the data we get back from the database is also in the correct charset.

For a full list of the different character sets available in MySQL just run the command:

SHOW CHARACTER SET;

This will display a table with the columns Charset, Description, Default collation and Maxlen. Each charset is associated with a collation. A collation is a set of rules for comparing characters in a charset, so it is important that you get this right if you want the database to work. The full list of collations can be viewed using the following command:

SHOW COLLATION;

You can even use a LIKE statement to refine the collation data into the information you are looking for.

SHOW COLLATION WHERE Charset LIKE '%utf%'

PHP Script To Turn Image Into ASCII Text

October 6th, 2008 Tech No comments

Use the following snippet to convert any jpeg image into the equivalent image in ASCII format. It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it. It then uses these values to loop through every pixel in the image and figures out the colour of that pixel. It uses this value to create a <span> element that uses the text colour of a # to change the colour of the text.

An additional time (and space) saver for this function is that it detects any pixels that are just off white and simply displays a &nbsp; character instead.

$img = ImageCreateFromJpeg('logo.jpg');
 
// get height and with of the image.
$width = imagesx($img);
$height = imagesy($img);
 
echo '<pre style="font-size:1px;">';
for($h=0;$h<$height;$h++){
 for($w=0;$w<=$width;$w++){
  if($w == $width){
   echo '<br>';
   continue;
  }
  // get image at pixel location
  $rgb = ImageColorAt($img, $w, $h);
  // convert colour into usable format
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8 ) & 0xFF;
  $b = $rgb & 0xFF;
  // check for white/off-white colour
  if($r > 200 && $g > 200 && $b > 200){
   echo '&nbsp';
  }else{
   echo '<span  style="color:rgb('.$r.','.$g.','.$b.');">#</span>';
  }
 }
}
echo '</pre>';

As an example, take the following, quite recognisable, image.

This is transformed into the text on the following page.

The Google logo in ASCII format

Beware that although this works will for small image sizes if you try to convert a very large image you will find that the script takes a long time. This is because it looks at every pixel in turn and converts the colour into something usable, so if there are a lot of pixels it takes a long time to look at every one. Not only this, but you will also find that the size of the page generated will grow significantly due to all of the span elements being created.

Categories: PHP Tags: , , , , ,

Convert A sitemap.xml File To A HTML Sitemap With PHP

August 13th, 2008 Tech No comments

I have already talked about converting a sitemap.xml file into a urllist.txt file, but what if you want to create a HTML sitemap? If you have a sitemap.xml file then you can use this to spider your site, scrape the contents of each page and populate the HTML file with this information.

The following code does this. For every page it looks for the <title> tag, the description meta tag and the first <h2> tag on the page. These items are then used to construct a segment of HTML for that page.

<?php
$header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>HTML Sitemap</title>
</head>
<body>';
 
set_time_limit(400);
 
$currentElement = '';
$currentLoc = '';
 
$map = "<h1>HTML Sitemap</h1>"."\n";
 
function parsePage($data){
 global $map;
 /*
 if you want to trap a certain file extention then use the syntax below...
 stripos($data,".php")>0
 stripos($data,".htm")>0
 stripos($data,".asp")>0
 */
 if(stripos($data,".pdf")>0){
  // if the url is a pdf document.
  $map .= '<p><a href="'.$data.'">PDF document.</a></p>'."\n";
  $map .= '<p>A pdf document.</p>'."\n";
 }elseif(stripos($data,".txt")>0){
  // if the url is a text document
  $map .= '<p><a href="'.$data.'">Text document.</a></p>'."\n";
  $map .= '<p>A text document.</p>'."\n";
 }else{
  // try to open it anyway...
  // make sure that you can read the file
  if($urlh = @fopen($data, 'rb')){
   $contents = '';
   //check php version
   if(phpversion()>5){
    $contents = stream_get_contents($urlh);
   }else{
    while(!feof($urlh)){
     $contents .= fread($urlh, 8192);
    };
   };
 
   // find the title
   preg_match('/(?<=\<[Tt][Ii][Tt][Ll][Ee]\>)\s*?(.*?)\s*?(?=\<\/[Tt][Ii][Tt][Ll][Ee]\>)/U',$contents,$title);
   $title = $title[0];
 
   // find the first h1 tag
   $header = array();
   preg_match('/(?<=\<[Hh]2\>)(.*?)(?=\<\/[Hh]2\>)/U',$contents,$header);
   $header = strip_tags($header[0]);
 
   if(strlen($title)>0 && strlen($header)>0){
    // print the title and h1 tag in combo
    $map .= '<p class="link"><a href="'.str_replace('&','&amp;',$data).'" title="'.(strlen($header)>0?trim($header):trim($title)).'">'.trim($title).(strlen($header)>0?" - ".trim($header):'').'</a></p>'."\n";
   }elseif(strlen($title)>0){
    $map .= '<p class="link"><a href="'.str_replace('&','&amp;',$data).'" title="'.trim($title).'">'.trim($title).'</a></p>'."\n";
   }elseif(strlen($header)>0){
    $map .= '<p class="link"><a href="'.str_replace('&','&amp;',$data).'" title="'.trim($header).'">'.trim($header).'</a></p>'."\n";
   };
 
   // find description
   preg_match('/(?<=\<[Mm][Ee][Tt][Aa]\s[Nn][Aa][Mm][Ee]\=\"[Dd]escription\" content\=\")(.*?)(?="\s*?\/?\>)/U',$contents,$description);
   $description = $description[0];
 
   // print description
   if(strlen($description)>0){
    $map .= '<p class="desc">'.trim($description).'</p>'."\n";
   };
   // close the file
   fclose($urlh);
  };
 };
};
 
/////////// XML PARSE FUNCTIONS HERE /////////////
// the start element function
function startElement($xmlParser,$name,$attribs){
 global $currentElement;
 $currentElement = $name;
};
 
// the end element function
function endElement($parser,$name){
 global $currentElement,$currentLoc;
 if($currentElement == 'loc'){
  parsePage($currentLoc);
  $currentLoc = '';
 };
 $currentElement = '';
};
 
// the character data function
function characterData($parser,$data){
 global $currentElement,$currentLoc;
 // if the current element is loc then it will be a url
 if($currentElement == 'loc'){
  $currentLoc .= $data;
 };
};
 
// create parse object
$xml_parser = xml_parser_create();
// turn off case folding!
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING, false);
// set start and end element functions
xml_set_element_handler($xml_parser,"startElement","endElement");
// set character data function
xml_set_character_data_handler($xml_parser,"characterData");
 
// open xml file
if(!($fp = fopen('sitemap.xml',"r"))){
 die("could not open XML input");
};
 
// read the file - print error if something went wrong.
while($data = fread($fp,4096)){
 if(!xml_parse($xml_parser,$data,feof($fp))){
  die(sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));
 };
};
 
// close file
fclose($fp);
 
$footer = '</body>
</html>';
 
// write output to a file
$fp = fopen('sitemap.html',"w+");
fwrite($fp,$header.$map.$footer);
fclose($fp);
 
// print output
echo $header.$map.$footer;
?>

This script prints out the sitemap and also saves the sitemap to a file for later use. This is essential as the script can take a long time to run due to all of the page accessing that it has to do.

This script is failry complicated and has gone through several versions since I first created it so if you find any improvements or bugs then let me know and I will incorporate them.

Categories: PHP Tags: , , , , , , ,

Convert A sitemap.xml File To A urllist.txt File Using PHP

August 12th, 2008 Tech No comments

If you create a script that produces a sitemap.xml file there is no point in adapting this script so that it creates a urllist.txt file. The best solution is to use this sitemap.xml file to create the urllist.txt. The following script will do exactly this.

$lines = file('sitemap.xml');
$allMatches = array();
 
foreach($lines as $line_number => $line){
 $line = trim($line);
 preg_match_all('/(?<=\<loc\>)(.*?)(?=\<\/loc\>)/U',$line,$matches,PREG_SET_ORDER);
 if($matches){
  if($matches[0][0] != ''){
   $allMatches[] = $matches[0][0];
  };
 };
};
 
$list = '';
foreach($allMatches as $url){
 $list .= $url."\n";
};
$fh = fopen('urllist.txt',"w+");
fwrite($fh,$list);
fclose($fh);
 
// print out list to provide some feedback…
echo $list;

The script works by first loading the sitemap.xml file into an array using the file() function. The script then goes through all of the items in the array and picks out everything between the <loc> tags and puts these into an array. It then adds these to a file called urllist.txt but also prints out the output to provide some indication that the script has run. This can be removed if you want to incorporate it into a larger script.

Categories: PHP Tags: , , , , ,