0x01: Lexical analysis of strings according to predefined characters
Copy Code code as follows:
<?php
/*
* When dealing with a large amount of information, the regular expression function slows down the speed significantly. You should use regular expressions to parse more complex strings when you need to use these functions. If you want to parse a simple expression, you can also use a number of predefined functions that can significantly speed up the process.
*/
/*
* Lexical analysis of strings based on predefined characters
* The Strtok () function parses a string based on a predefined list of characters. In the form of:
* String Strtok (string str,string tokens)
* Strtok () function, the function must be called continuously in order to complete the lexical analysis of a string, each call the function is only the next part of the string to do lexical analysis. However, the STR parameter only needs to be specified once, because the function tracks the position in STR, knows that the lexical analysis is complete for STR, or that the experience str parameter is specified.
* As shown in the following example:
*/
$info = "LV Chen Yang|" Hello:world&757104454@qq.com ";
Define the definition character, including (|) (:) () (&)
$tokens = "|:&";
$tokened =strtok ($info, $tokens);
while ($tokened)
{
echo "Element: $tokened <br/>";
Continuous invocation of the strtok () function to complete the lexical analysis of the entire string
$tokened =strtok ($tokens);
}
?>
0X02: Decomposing strings according to predefined delimiters
Copy Code code as follows:
<?php
/*
* Decomposition of strings according to predefined delimiters: explode () function
* The secondary function divides the string str into a substring array in the form of:
* Array explode (string separator,string str [, int limit])
* The original string is divided into different elements according to the string specified by separator. The number of elements can be limited by an optional parameter limit. You can combine explode ()/sizeof () and Strip_tags () to determine the total number of words in a given block of text
* As shown below:
*/
$summary = "
In the latest installment of the ongoing Developer.com PHP series.
I discuss the many improvements and addtions to
<a href=\ "http:www.php.com\" >PHP</a> object-oriented architecture.
";
echo "<br/>";
$words =explode ("", Strip_tags ($summary));
echo "This sentence ' s lenght is:". sizeof ($words);
/*
* The explode () function is always much faster than Preg_split, spilt (), and Spliti (). Therefore, be sure to use this function when you do not need to use regular expressions.
*/
?>
0X03: Converting an array to a string
Copy Code code as follows:
<?php
/*
* Convert an array to a string
* The explode () function converts a string to a corresponding array based on the defined character, but can convert the array to a string with the bounds of the specified delimited character by the implode () function
* In the form of:
* String Implode (string Delimiter,array pieces)
* As shown below:
*/
$citys =array ("Chengdu", "Chongqing", "Beijing", "Shanghai", "Guangzhou");
$citystring =implode ("|", $citys);
Echo $citystring;
?>
0X04: Parsing a complex string
Copy Code code as follows:
<?php
/*
* Parse a complex string
* The Strpos () function finds the first occurrence of a substr in a string in a case-sensitive manner, in the form of
* int Strpos (string str,string substr [, int offset])
* Optional parameter offset specifies where to start the search. If SUBSTR is not in Str, Strpos () returns false. An optional parameter determines where Strpos () starts the search.
* The following example will determine the timestamp of the first access index.html:
*/
$substr = "index.html";
$log =<<<logfile
192.168.1.1:/WWW/HTDOCS/INDEX.HTML:[2013/06/26:13:25:10]
192.168.1.2:/WWW/HTDOCS/INDEX.HTML:[2013/06/26:13:27:16]
192.168.1.3:/WWW/HTDOCS/INDEX.HTML:[2013/06/26:13:28:45]
LogFile
echo "<br/>";
What is the position $substr first appeared in the log?
$pos =strpos ($log, $SUBSTR);
Find a numeric position at the end of a row
$pos 1=strpos ($log, "\ n", $pos);
Calculate the start of a timestamp
$pos = $pos +strlen ($substr) +1;
Retrieving time stamps
$timestamp =substr ($log, $pos, $pos 1-$pos);
echo "The file index.html was-accessed on: $timestamp <br/>";
/*
* Function Stripos () and function Strpos () function usage is the same, the only difference is that Stripos () is case-insensitive.
*/
?>
0x05: Find where the string last appeared
Copy Code code as follows:
<?php
/*
* Find the last occurrence in the string
* The Strrpos () function searches for the last occurrence of the string, returning its position (numeric number) in the form of:
* int Strrpos (string Str,char substr [, offset])
* Optional parameter offset determines the start search location of the Strrpos () function. Join in the hope of shortening the lengthy summary of the news,
* Intercept some parts of the summary and replace the truncated part with the ellipsis. However, it is not simple to summarize the definite cut to the desired length,
* You may want to cut in a user-friendly manner and intercept to the end of the word closest to the stage length.
* As shown in the following example
*/
$limit = 100;
$summary = "In the latest installment of the ongoing Developer.com PHP series.
I discuss the many improvements and addtions to
<a href=\ "http:www.php.com\" >PHP</a> object-oriented architecture. ";
if (strlen ($summary) > $limit)
$summary =substr ($summary, 0,strrpos (substr ($summary, 0, $limit), ""). " ...";
Echo $summary;
?>
0X06: Replaces all instances of a string with another string
Copy Code code as follows:
<?php
/*
* Replace all instances of the string with a different string
* The Str_replace () function uses another string to fantasy all instances of a string in a case-sensitive way. In the form of:
* Mixed Str_replace (string occurrence, mixed replacement, mixed str [, int count])
* If occurrence is not found in STR, STR remains unchanged, and if the optional parameter count is defined, only the Count currence in STR is replaced.
* This function is very suitable for the automatic access to the e-mail address of the program to hide the electronic right key address, as follows:
*/
$email = "lvchenyang@live.cn";
$email =str_replace ("@", "(at)", $email);
echo "<br/>". $email;
?>
0x07: Getting part of a string
Copy Code code as follows:
<?php
/*
* Get part of a string
The * STRSTR () function returns the remainder of the string from the first occurrence of the predefined string (including the occurrence string). In the form of:
* String Strstr (string str,string occurrence[,bool Fefore_needle])
* Optional parameter Before_needle will change the behavior of the STRSTR () so that the function returns the string before the first out of the section.
* The following example is to get the domain name in the right key, combining the LTrim () function
*/
$url = "lvchenyang@live.cn";
echo "<br/>". LTrim (Strstr ($url, "@"), "@");
?>
0X08: Returns part of a string according to the predefined cheapness
Copy Code code as follows:
<?php
/*
The * SUBSTR () function returns the portion of the string between start and start+length in the form of:
* String substr (String str,int start [, int length])
* Returns a string from start to str if no optional arguments are specified
* as shown below
*/
$str = "Lvchenyang";
echo "<br/>". substr ($str, 2,4);
Output:chen
?>
0X09: Determines how often the string appears
Copy Code code as follows:
<?php
/*
* Determine how often the string appears
* Substr_count () returns the number of occurrences of a string in another string. In the form of:
* int Substr_count (string str,string substring [, int offset [, int length]])
* Optional parameter offset and length specify the string to be cheap (try to match the string from a cheap start) and the length of the string (the length of the search from a cheap start)
* The following example determines the number of times each word appears in this sentence
*/
$talk =<<<talk
I am acertain that "we could dominate mindshare in" with
Our new product, extablishing a true synergy Beteen the marketing
and product development teams. We ' ll own this spaces in thress months.
Talk
echo "<br/>";
$sentencearray =explode ("", $talk);
foreach ($sentencearray as $item)
{
echo "The word <strong> $item </strong> appears (". Substr_count ($talk, $item). ") Times<br/> ";
}
?>
0X10: Replaces part of a string with another string
Copy Code code as follows:
<?php
/*
* Replace part of a string with another string
* The Substr_replace () function replaces a portion of a string with another string, starting at the specified start position and knowing that the start+length position ends.
* In the form of:
* Stringsubstr_replace (String str,string repalcement,int the value of start and length.
* As shown below, replace 4 digits in the middle of the telephone number
*/
$phonenum = "15926841384";
echo "<br/>" Substr_replace ($phonenum, "* * *", 3,4);
?>