| |
String/Integer - PHP |
String/Integer JavaScript |
| Is String? |
is_string($str); |
typeof(str)=="string"; |
| Is Integer or Float |
is_int ( mixed $var );
is_numeric ( mixed $var );
is_float ( mixed $var ); |
isNaN(integ);
Example:
document.write(isNaN(348)) produces: false |
| String Length |
strlen($str); |
str.length; |
| Convert to Upper or Lower |
strtolower(str);
strtoupper(str); |
str.toLowerCase();
str.toUpperCase(); |
| Replace text within a portion of a string |
str_replace( $look-for-this-text, $replace-it-with-this-text,
$str );
Example:
$bodytag = str_replace("%body%", "black",
"<body text='%body%'>");
Produces: <body text='black'> |
str.replace( look-for-this-text, replace-it-with-this-text
); |
Return part of a string |
substr($str, $start, $length);
Example:
echo substr('abcdef', 1); produces: bcdef
echo substr('abcdef', 1, 3); produces: bcd |
str.substr(int start, int lenght);
var str = 'Hello world!';
alert(str.substring(0,4)); produces 'Hell'
str.substring(int start, int end));
var str = 'Hello world!';
alert(str.substring(1,4)); produces 'ello' |
Return a character at a certain position |
substr($str, $position, $length=1);
Example:
echo substr('Hello World', 6, 1); produces: W |
str.charAt(char);
Example:
var hello="Hello World";
var char=hello.charAt(6); produces: W
alert('The 6th character is ' + char + '.'); |
| Find position # of first occurrence of a string |
strpos('abc', 'b'); produces: 1
strpos( 'abcdef abcdef', 'a', 1); produces 7, not 0 |
var str = 'Hello world';
positionofW = str.indexOf('W');
document.write(positionofW); produces: 6
alert('The a is at position ' + positionofW + '.'); |
| Find first occurrence of a string |
strstr($stack, $needle, bool $before_needle)
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
$user = strstr($email, '@', true);
echo $user; // prints name |
stack.substring( stack.indexOf(needle) ); |
| To Split String into Arrays |
explode(splitter, str);
Example:
$str = 'Hello Array World';
$strarray = new Array();
$strarray = explode(" ", $str);
$strarray[0] = 'Hello';
$strarray[1] = 'Array';
$strarray[2] = 'World'; |
str.split(splitter);
Example:
var str = 'Hello Array World';
var strarray = new Array();
strarray = str.split( " ");
strarray[0] = 'Hello';
strarray[1] = 'Array';
strarray[2] = 'World'; |
| Replace text within a portion of a string |
substr_replace( $str, $replacement, $index, $length ); |
str.replace( str.substring( index, (index+length) ), replacement
); |
| Strip HTML and PHP tags from a string |
$stripstr=striptags($str); |
stripstr=str.replace(/<[^>]+>/g, ''); |