PHP character functions

PHP produces the stochastic string of character the function, below is I in 2 which on-line found about the PHP stochastic string of character function, hoped that everybody liked.

First example this used php shuffle to disrupt the array function

PHP:

<? php
function genRandomString($len)
{
$chars = array (
“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”,
“l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”,
“w”, “x”, “y”, “z”, “A”, “B”, “C”, “D”, “E”, “F”, “G”,
“H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”,
“S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”, “0”, “1”, “2”,
“3”, “4”, “5”, “6”, “7”, “8”, “9”
);
$charsLen = count($chars) - 1;
shuffle($chars);  // disrupts the array
$output = "";
for ($i=0; $i<$len; $i++)
{
$output. = $chars [mt_rand (0, $charsLen)];
}
return $output;
}
$str = genRandomString(25);
$str. = “<br/>”;
$str. = genRandomString(25);
$str. = “<br/>”;
$str. = genRandomString(25);
echo $str;
? >

Second demonstration this used the php rand random function

PHP:

<? php
/* Generate Password
* Length: 8
*/
$str = “0123456789abcdefghijklmnopqrstuvwxyz”; // output character repertoire
$n = 8; // output string length
$len = strlen($str)-1;
for ($j=0; $j<200; $j++) {
for ($i=0; $i<$n; $i++) {
$s. = $str[rand(0,$len)];
}
echo $s. “<br/>”;
$s = "";
}
? >

10 January 2010 at 08:02 - Comments

PHP: include() and require() contrast

php require() performance and include() similar.

The difference lies, to include(), when the include() execution the document each time must carry on the read and the appraisal; But regarding require(), the document only processes one time (in fact, document content has replaced the require() sentence).

This means that if has contains of a code these instructions and possible to carry out the multiple code, then uses the require() efficiency to be quite high. On the other hand, if each time carries out the code phase read different document, or has through a group of document iteration circulation, uses include(), because may include to the wish the filename establishes a variable, when the parameter is include() uses this variable.


8 January 2010 at 07:59 - Comments

Used to achieve a simple tree menu php

<style>
table (font-size = 9pt)
td (height = 10px)
</ style>
<body>
<span id=”menus”> </ span>
<span id=”view”> </ span>
</ body>
<script>
/ **
* Structure tree, the initial value of 0
* /
function tree (n) (
var id = new Array ( “bar”, “pad “,”#”,”+”);
if (n == 0) (/ / Initialize variables
n = 1;
i = 0;
s = “”;
)
s + = “<table>”;
for (; i <tree_ar.length-1; i + +) (
var k = (n> = tree_ar [i +1] [0])? 0:1;
s + = “<tr id=’”+id[k]+”‘ value=”+i+”> <td>” + id [k +2 ]+”</ td> <td> “+ tree_ar [i ] [1 ]+”</ td> </ tr> “; / / construct node, pay attention to where custom attributes value. Role is to simplify the description of the node structure, shared parameters of the array of information.
if (n> tree_ar [i +1] [0]) (/ / if the expectation level is greater than the current level, the end of this level of return to the previous level of the hierarchy.
s + = “</ td> </ tr> </ table>”;
return tree_ar [i +1] [0];
)
if (n <tree_ar [i +1] [0]) (/ / if the expectation level is less than the current level of recursion to the next level.
s + = “<tr style=’display:none’ v=1> <td> </ td> <td>”;
var m = tree (tree_ar [+ + i] [0]);
s + = “</ td> </ tr>”;
if (m <n) (/ / if return value is less than the current level of recursion expectations, it will generate a continuous return to action.
s + = “</ table>”;
return m;
)
)
)
s + = “</ table>”;
return s;
)
</ script>
<script for=pad event=onclick>
/ / Branch nodes Click to respond to
v = this.parentElement.rows [this.rowIndex +1]. style;
if (v.display == ‘block’) (
v.display = ‘none’;
this.cells [0]. innerHTML = “+”;
view.innerHTML = “”; / / modify the definition of closure as a parameter an array of actions
) else (
v.display = ‘block’;
this.cells [0]. innerHTML = “-”;
view.innerHTML = “<b>” + tree_ar [this.value] [1 ]+”</ b> “; / / modify the definition of a parameter array of the unfolding action
)
/ **
* The following code is used to shut down the other branch has embarked on
* For a self-started branching off from here, or delete the code directly back
* /
if (! tree_ar [this.value]. type) / / If the node is the first time, then the record of where the level of information
genTreeInfo (this);
var n = 1 * this.value +1;
for (i = n; i <tree_ar.length-1; i + +) (/ / close the array after the current node in the tree
if (tree_ar [i]. type == “pad”) (
tree_ar [i]. obj2.style.display = ‘none’;
tree_ar [i]. obj1.cells [0]. innerHTML = “+”;
)
)
while (tree_ar [- n] [0]> 1); / / back to the starting point of the current tree
while (- n> = 0) / / close the array in the current starting point of the tree before the tree
if (tree_ar [n]. type == “pad”) (
tree_ar [n]. obj2.style.display = ‘none’;
tree_ar [n]. obj1.cells [0]. innerHTML = “+”;
)
/ ** Record levels of information, to simplify the complexity of traversing the tree nodes when the description ** /
function genTreeInfo (o) (
var el = o.parentElement;
for (var i = 0; i <el.rows.length; i + +) (
if (el.rows [i]. id! = “”) (
tree_ar [el.rows [i]. value]. type = el.rows [i]. id;
)
if (el.rows [i]. id == “pad”) (
tree_ar [el.rows [i]. value]. obj1 = el.rows [i];
tree_ar [el.rows [i]. value]. obj2 = el.rows [i +1];
)
)
)
</ script>
<script for=bar event=onclick>
/ / No branch nodes Click to respond to
view.innerHTML = “<b>” + tree_ar [this.value] [1 ]+”</ b> “; / / modify the definition of a parameter array of clicks
</ script>
<script>
/ **
* An array of basic parameters, according to its own specific application extensions
* Data can be relatively simple provided by the server-side
* Column 1: node-level
* Column 2: Node Title
* The remaining self-expansion
* /
tree_ar = new Array (
new Array (1, “Node 1″),
new Array (1, “Node 2″),
new Array (2, “Node 3″),
new Array (2, “Node 4″),
new Array (3, “Node 5″),
new Array (4, “Node 6″),
new Array (5, “node 7″),
new Array (6, “Node 8″),
new Array (7, “Node 9″),
new Array (2, “node 10″),
new Array (1, “node 11″),
new Array (2, “node 12″),
new Array (2, “node 13″),
new Array (1, “node 14″),
new Array (1, “”) / / In order to simplify the termination of the empty data items to determine the additional
);
/ *** Create a menu *** /
menus.innerHTML = tree (0);
</ script>

5 January 2010 at 13:58 - Comments

PHP function: Paging (two functions)

function getpage($sql,$page_size=20)
{
global $page,$totalpage,$sums; //out param
$page = $_GET["page"];
//$eachpage = $page_size;
$pagesql = strstr($sql,” from “);
$pagesql = “select count(*) as ids “.$pagesql;
$result = mysql_query($pagesql);
if($rs = mysql_fetch_array($result)) $sums = $rs[0];
$totalpage = ceil($sums/$page_size);
if((!$page)||($page<1)) $page=1;
$startpos = ($page-1)*$page_size;
$sql .=” limit $startpos,$page_size “;
return $sql;
}

function showbar($string=”")
{  
global $page,$totalpage;
$out=”共<font color=’red’><b>”.$totalpage.”</b></font>页&nbsp;&nbsp;”;
$linkNum =4;
$start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : “1″;
$end  = ($page+round($linkNum/2))<$totalpage ? ($page+round($linkNum/2)) : $totalpage;
$prestart=$start-1;
$nextend=$end+1;
if($page<>1)
$out .= “<a href=’?page=1&&”.$string.”‘title=first>first page</a>&nbsp;”;
if($start>1)
$out.=”<a href=’?page=”.$prestart.”‘ title=>..<<</a>&nbsp;”;
for($t=$start;$t<=$end;$t++)
{
$out .= ($page==$t) ? “<font color=’red’><b>[".$t."]</b></font>&nbsp;” : “<a href=’?page=$t&&”.$string.”‘>$t</a>&nbsp;”;
}
if($end<$totalpage)
$out.=”<a href=’?page=”.$nextend.”&&”.$string.”‘ title=>>>..</a>”;
if($page<>$totalpage)
$out .= “&nbsp;<a href=’?page=”.$totalpage.”&&”.$string.”‘ title=last>last</a>”;
return $out;
}
?>

1 January 2010 at 16:51 - Comments

PHP function:. To create the appropriate folders

function createdir($dir=”)
{
if (!is_dir($dir))
{
$temp = explode(’/',$dir);
$cur_dir = ”;
for($i=0;$i<count($temp);$i++)
{
$cur_dir .= $temp[$i].’/';
if (!is_dir($cur_dir))
{
@mkdir($cur_dir,0777);
}
}
}
}

30 December 2009 at 07:50 - Comments

PHP function: to determine e-mail address

function checkEmail($inAddress)
{
return (ereg(”^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+”,$inAddress));

}

29 December 2009 at 07:45 - Comments

Php function: Interception of a certain length of the string

function createdir($dir=”)
{
if (!is_dir($dir))
{
$temp = explode(’/',$dir);
$cur_dir = ”;
for($i=0;$i<count($temp);$i++)
{
$cur_dir .= $temp[$i].’/';
if (!is_dir($cur_dir))
{
@mkdir($cur_dir,0777);
}
}
}
}

28 December 2009 at 07:44 - Comments

PHP function: obtain the client IP address

function GetIP(){
if (getenv(”HTTP_CLIENT_IP”) && strcasecmp(getenv(”HTTP_CLIENT_IP”), “unknown”))
$ip = getenv(”HTTP_CLIENT_IP”);
else if (getenv(”HTTP_X_FORWARDED_FOR”) && strcasecmp(getenv(”HTTP_X_FORWARDED_FOR”), “unknown”))
$ip = getenv(”HTTP_X_FORWARDED_FOR”);
else if (getenv(”REMOTE_ADDR”) && strcasecmp(getenv(”REMOTE_ADDR”), “unknown”))
$ip = getenv(”REMOTE_ADDR”);
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], “unknown”))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = “unknown”;
return($ip);
}

27 December 2009 at 07:42 - Comments

How to use PHP’s string functions

PHP’s string manipulation function is relatively more important are the following:

(1) echo, print, printf, sprintf

The first two functions are the output string. String if there are variable names were replaced by its value.

The latter two functions similar to the C function of the same name.

(2) strchr, strlen, strtok, strrchr, strrev, strstr, strtolower,

strtoupper, substr, ucfirst

These are commonly used string manipulation functions, some of the same name, and C, the function exactly the same meaning.

strrev is to flip a string.

strtolower and strtoupper meaning should not have to explain.

ucfirst is to the first character string into upper case.

substr is to return a string sub-string, usage is: substr (string, head, length).

The first location is from 0 to date. If is negative, this is the meaning of the number from the rear forward.

(3) Chr, Ord

Similar to the C function of the same name.

(4) explode, implode, join

These are and array-related functions.

explode (string separator) returns a string in the separator produced by an array of actions to open.

implode (array, separator) returns an array of elements formed between the plug in the separator string.

join and implode same meaning.

(5) Chop

Remove trailing blank string.

(6) htmlspecialchars

The string in the HTML special characters replaced by their names, such as “” “becomes” “.”

(7) nl2br

In the string in front of each carriage return with a “<BR>”.

(8) AddSlashes, StripSlashes

Respectively, to the string that need to add “” can be used for database query add and remove the characters “.”

(9) parse_str

Will be “name1 = value1 & name2 = value2 &…” type of string parsing into a number of variables.

For example:

parse_str ( “a = 1 & b = 2″);

Generated $ a and $ b are two variables, respectively 1,2.

If there are two pairs of name / value of the name of some of the same, the latter one the value of the coverage of the previous one.

If the two pairs of the names of the tail has a “[]“, example “a [] = 1 & a [] = 2″, then generate an array $ a, two elements 1,2 respectively

27 December 2009 at 07:38 - Comments

Output Buffer function(Output Buffer)

In PHP programming, we often encounter a direct result of the output function, such as passthru (), readfile (), var_dump () and so on. But sometimes we want these functions, into the output file, or first treated and then output, or the output of these functions as strings to deal with.

At this time we have to use Output Buffer (output buffer) functions of.

Dealing with the main function of the output buffer has the following:

ob_start () Start the output buffer, then PHP stopped output, after which the output is transferred to an internal buffer where.

ob_get_contents () This function returns the contents of the internal buffer. This is equivalent to these outputs have become a string.

ob_get_ length () Returns the length of the internal buffer.

ob_end_flush () end of the output buffer and output buffer during the session. In the following output is normal output.

ob_end_clean () end of the output buffer, and throw away the contents of the buffer inside.

For example, var_dump () function, the output of a variable structure and content, which is useful when debugging.

However, if the contents of the variable there “,” special characters such as HTML, the output to the page where the sight. How do?

With the output buffering functions can easily solve this problem.

ob_start ();

var_dump ($ var);

$ out = ob_get_contents ();

ob_end_clean ();

Then var_dump () output already exists $ out inside. You can now output:

echo ‘<pre>’. htmlspecialchars ($ out). ‘</ pre>’;

Or wait until the future, then this string is sent to the template, or (Template) where re-export

26 December 2009 at 07:36 - Comments
Powered by Yahoo! Answers