Author Archives: author

How to redirect iPone users using PHP

// Redirect iPhone/iPod visitors
if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') ||
   strpos($_SERVER['HTTP_USER_AGENT'], 'iPod')){
      header("Location: http://mob.example.com");
}

User agent string for reference

//*
User Agent String for iPhone
    Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko)
    Version/3.0 Mobile/1A543a Safari/419.3
    
User Agent String for iPod Touch
    Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko)
    Version/3.0 Mobile/3A101a Safari/419.3
*/

Javascript: URL Validation

Javascript: url validating function

[code language="javascript"]

function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
}

[/code]

usage:
[code language="javascript"]
if (isUrl("http://www.page.com"))
alert("is correct")
else
alert("not correct");
[/code]

JavaScript Switch Statement

JavaScript Switch Statement

Syntax

[code language="javascript"]
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
[/code]

Example

[code language="javascript"]
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.

var d=new Date();
var theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("Finally Friday");
break;
case 6:
document.write("Super Saturday");
break;
case 0:
document.write("Sleepy Sunday");
break;
default:
document.write("I'm looking forward to this weekend!");
}
</script>
[/code]

Info: To look into

[php]
while (list($key,$value)=each($parameters_array))
{
$$key=$value;
}
[/php]

handy little bit of code I wrote to take arguments from the command line and parse them for use in my apps.

[php]
<?php

$i = implode(" ",$argv); //implode all the settings sent via clie
$e = explode("-",$i); // no lets explode it using our defined seperator ‘-’

//now lets parse the array and return the parameter name and its setting
// since the input is being sent by the user via the command line
//we will use stristr since we don’t care about case sensitivity and
//will convert them as needed later.

while (list($index,$value) = each($e)){

//lets grap the parameter name first using a double reverse string
// to get the begining of the string in the array then reverse it again
// to set it back. we will also "trim" off the "=" sign

$param = rtrim(strrev(stristr(strrev($value),’=')),"=");

//now lets get what the parameter is set to.
// again "trimming" off the = sign

$setting = ltrim(stristr($value,’='),"=");

// now do something with our results.
// let’s just echo them out so we can see that everything is working

echo "Array index is ".$index." and value is ".$value."\r\n";
echo "Parameter is ".$param." and is set to ".$setting."\r\n\r\n";

}

?>
[/php]

when run from the CLI this script returns the following.

[root@fedora4 ~]# php a.php -val1=one -val2=two -val3=three

Array index is 0 and value is a.php
Parameter is and is set to

Array index is 1 and value is val1=one
Parameter is val1 and is set to one

Array index is 2 and value is val2=two
Parameter is val2 and is set to two

Array index is 3 and value is val3=three
Parameter is val3 and is set to three

List of string functions in php


All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Converting first letter of every word to UpperCase

Disable a Hyperlink using JQuery


$('.my-link').click(function(e) { e.preventDefault(); }); 

You could use:

$('.my-link').click(function(e) { return false; });


$(".my-link").each(function() {
    $(this).attr("data-oldhref", $(this).attr("href"));
    $(this).removeAttr("href");
});


$('.my-link').bind('click', false);

And to unbind / re-enable:

$('.my-link').unbind('click', false);

JQuery: How to get value of ckecked checkbox

JQuery: How to get value of ckecked checkbox

[javascript]
function getCheckedId()
{
var allVals = [];
$(&quot;input[type='checkbox']:checked&quot;).each(function() { allVals.push($(this).val());
});

if( ! allVals ) return false;

len = allVals.length;

if( len == 0) return 0;

if (len &amp;gt;1) {
alert(&quot;More than one vehicle selected. Only one will be processed. &quot;);
}

return allVals[0];
}

[/javascript]

UI Dialog Positioning

jQuery UI dialog positioning – Stack Overflow

[javascript]
$(&quot;.mytext&quot;).mouseover(function() {
var width = 250;
var height = 270;
var posX = $(this).offset().left – $(document).scrollLeft() – width + $(this).outerWidth();
var posY = $(this).offset().top – $(document).scrollTop() + $(this).outerHeight();
$(&quot;#dialog&quot;).dialog({width:width, height:height ,position:[posX, posY]});
}
[/javascript]