Posts Tagged ‘String’

implode vs join in php

Tuesday, July 15th, 2008

This one really surprised me. I expected these functions to be faster. Both these functions allow you to convert an array into a string. implode is the opposite of explode and join allows you to join the items in an array with a ‘glue’ string. In this case the array had 100 items.

implode vs join
implode: 47.2712550163 seconds
join: 50.1287050247 seconds
Time saved: 2.85745000839 seconds; 5.70022705949%

5% doesn’t seem like all that much but 2 microseconds shouldn’t be ignored.

PHP:Use “sprintf”

Monday, June 30th, 2008

Example 1.Uncleanly Spliced String.

<?php

$sql = “SELECT col1, col2, col3 FROM people WHERE first_name = ‘” . mysql_real_escape_string($first_name) . “‘ AND last_name = ‘” . mysql_real_escape_string($last_name) . “‘AND foo = ‘” . ($bar = “good” ? “good” : “bad”) . “‘ ORDER BY col1″ ;

?>
I see these types of indecipherable strings –like this SQL statement example– all too frequently. The meaning of this query has been lost due to numerous concatenations and escape functions; which means developers have to invest a significant amount of time to comprehend the code.

In order to avoid this this, I use PHP’s sprintf() function. sprintf() is a function that’s part of a family of functions –referred to as the “printf” family of function– that substitute designated tokens with arguments to the function. For example, let’s look at the code in example 3.2.
Example 2: Cleanly Assembled String With sprintf()

<?php

$sql = ‘SELECT col1, col2, col3 ‘ .
‘FROM people ‘ .
‘WHERE first_name = “%s” ‘ .
‘AND last_name = “%s” ‘ .
‘AND foo = “%s” ‘ .
‘ORDER BY col1 ‘;

$sql = sprintf($sql, mysql_real_escape_string($first_name),
mysql_real_escape_string($last_name),
($bar = “good” ? “good” : “bad”));

?>

This method allows developers to regain a sense of the data that they’re representing. In this particular example the %s token means replace with a string. There a number tokens such as %d (decimal) and %f (floating point).

Javascript:How to convert string to ASCII number sequence in JavaScript

Monday, June 30th, 2008

<script type=”text/javascript”>
var str = “Hello World!”
var res = “”
for (i=0;i < str.length; i++) {
res += str.charCodeAt(i) + ‘,’;
}
res = res.substr(0, res.length - 1);
//Result: 72,101,108,108,111,32,87,111,114,108,100,33
document.write(res);
</script>

How to convert ASCII number to string in JavaScript

Monday, June 30th, 2008

<script type=”text/javascript”>
// outputs: “Hello World!”
document.write(String.fromCharCode(72,101,108,108,111,32,87,111,114,108,100,33));
</script>

Remove empty strings/elements from a PHP array

Monday, June 30th, 2008

<?php
$data = array(
‘  ‘,
‘test’,
123,
);
// filtering non-empty elements.
$data = preg_grep(’#S#’, array_map(’trim’, $data));
var_dump($data);
/* Result
array(2) {
[1]=>
string(4) “test”
[2]=>
string(3) “123″
}
*/
?>

PHP:Validate string length

Monday, June 30th, 2008

// str len validator
function str_validator( $str, $min, $max )
{
if ( strlen( $str ) >= $min && strlen( $str ) <= $max )
{
return true;
} else
{
return false;
}
}

PHP:Validate URL using outgoing connection ( fopen )

Monday, June 30th, 2008

<?php
/**
* valid_url - validates supplied url/uri making a connection
*
* Note:
*
* 1) this method is more accurate than a validation made by a regular expression but it is much slower
* 2) administraters may restrict outgoing connections from your hosting to outside world and the function may raise a PHP Warning
*
* @param $url String
* @return true if the address is valid
*
*/
function valid_url( $url )
{
if ( @fopen( $url, ‘r’ ) )
{
return true;
} else
{
return false;
}
}
if ( valid_url( “http://blog.tryangled.com” ) )
{
echo “URL OK”;
} else
{
echo “Invalid URL.”;
}
?>

Change Internet Explorer’s Caption

Wednesday, June 18th, 2008

Don’t like the caption of Internet Explorer caption? Want to change it? Open the registry editor and go to

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main.

In the right pane create a new String Value names Window Title (Note the space between Window and Title). Right click on this newly created String Value and select Modify. Type in the new caption you want to be displayed. Restart for the settings to take place.

Now let’s move on to some Outlook Express Tricks.

Making the Internet Explorer & the Explorer Toolbars Fancy

Wednesday, June 18th, 2008

The Internet Explorer toolbar looks pretty simple. Want to make it fancy and kewl? Why not add a background image to it. To do this kewl hack launch the Windows Registry Editor and go to the following key: HKEY_CURRENT_USER\SOFTWARE\Microsoft\ Internet Explorer\Toolbar\.

Now in the right pane create a new String Value and name it BackBitmap and modify it’s value to the path of the Bitmap you want to dress it up with by rightclicking on it and choosing Modify. When you reboot the Internet Explorer and the Windows Explorer toolbars will have a new look.