Monthly Archives: December 2010 - Page 2

What are \r and \n meaning in PHP?

1.They’re escape sequences. \n is a newline and \r is a carriage return.

In Windows most text editors have a newline as \r\n and on unix it’s \n.

2.\r is the carriage return

\n is the newline

These are available in many other languages aside from PHP.

3.They’re “carriage return” and “line feed” respectively. Typically on Windows, you need both together to represent a line terminator: “\r\n” whereas on most (all?) Unix systems, “\n” is enough.

PHP- How to get the ip address of the website

<?php

echo gethostbyname(‘www.phpfresher.com’);

?>

PHP- String function explanation

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

Random text(string) generation using PHP

First i would like to introduce two string function:

1 .Chr() function

The chr() function returns a character from the specified ASCII value.

syntax: chr(ascii)

How to use the chr() function:

<?php
echo chr(52).”<br />”;
echo chr(052).”<br />”;
echo chr(0x52).”<br />”;
?>

The output of the code above will be:

4
*
R

2 . rand() function

The rand() function generates a random integer.

If this function is called without parameters, it returns a random integer between 0 and RAND_MAX.

If you want a random number between 10 and 100 (inclusive), use rand (10,100).

Syntax

rand(min,max)

Example

<?php
echo(rand(10,100))
?>

Output:

67(or the number between 10 and 100)

ok lets move to the real tutorial. This tutorial will teach you how to generate the random string.

Coding:

<?

$lengthofstring = 10;
while($i < $lengthofstring ) {
$x=1;
$y=3;
$part = rand($x,$y); //returns either 1 or 2 or 3
if($part==1){$a=48;$b=57;}  // Numbers
if($part==2){$a=65;$b=90;}  // UpperCase
if($part==3){$a=97;$b=122;} // LowerCase
$code_part=chr(rand($a,$b));  // returns the character for the randomly generated character
$i++;
$newstring = $newstring.$code_part;
}
echo $newstring;
?>

Try this out…..