Some string manipulation functions in PHP that can replace regular expression functions, PHP regular expressions
0x01: Lexical analysis of strings based on predefined characters
Copy the Code code as follows:
<?php
/*
* Regular expression functions slow down greatly when handling large amounts of information. You should use these functions when you need to parse more complex strings using regular expressions. 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 pre-defined characters
* The Strtok () function parses a string based on a list of predefined characters. The form is:
* String Strtok (string str,string tokens)
* Strtok () function, you must call this function continuously to complete the lexical analysis of a string, each call to the function is only a lexical analysis of the next part of the string. However, the STR parameter only needs to be specified once, because the function tracks the position in Str, knowing that the lexical analysis is complete for STR, or that it specifies the experience str parameter.
* As shown in the following example:
*/
$info = "LV Chen Yang| Hello:world&757104454@qq.com ";
Define the delimiter, including (|) (:) () (&)
$tokens = "|:&";
$tokened =strtok ($info, $tokens);
while ($tokened)
{
echo "Element: $tokened
";
Continuous invocation of the strtok () function to complete lexical analysis of the entire string
$tokened =strtok ($tokens);
}
?>
0x02: Decomposing a string according to a predefined delimiter
Copy the 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 the 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
PHP object-oriented Architecture.
";
echo "
";
$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 into a string
Copy the Code code as follows:
<?php
/*
* Convert an array into a string
* The explode () function converts a string to a corresponding array based on a defined character, but can convert an array to a specified delimited string 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 Complex strings
Copy the Code code as follows:
<?php
/*
* Parse Complex strings
* 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, then 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 =<<<>
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 "
";
What is the first occurrence of $substr in log
$pos =strpos ($log, $SUBSTR);
Find the value position of the end of the line
$pos 1=strpos ($log, "\ n", $pos);
Calculate the start of a timestamp
$pos = $pos +strlen ($substr) +1;
Retrieving timestamps
$timestamp =substr ($log, $pos, $pos 1-$pos);
echo "The file index.html is first accessed on: $timestamp
";
/*
* Function Stripos () and function Strpos () function usage are the same, the only difference is that Stripos () is not case-sensitive.
*/
?>
0x05: Find the last occurrence of the string
Copy the Code code as follows:
<?php
/*
* Find the last occurrence of the string in the position
* The Strrpos () function searches for the last occurrence of a string and returns its position (numeric number) in the form of:
* int Strrpos (string Str,char substr [, offset])
* Optional parameter offset determines the start search position of the Strrpos () function. Join the hope of shortening the lengthy news summary,
* Intercept some parts of the summary and use ellipses instead of the section to be truncated. However, it is not simple to summarize a definite cut to the desired length,
* You may want to cut in a user-friendly way, capturing the end of the word closest to the length of the stage.
* As shown in the example below
*/
$limit = 100;
$summary = "In the latest installment of the ongoing Developer.com PHP series.
I discuss the many improvements and addtions to
PHP 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 the Code code as follows:
<?php
/*
* Replace all instances of a string with another string
* The Str_replace () function uses a different string to match all instances of a string in a case-sensitive manner. The form is:
* 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 count currence in Str are replaced.
* This function is ideal for hiding electronic right-click addresses for programs that automatically get e-mail addresses, as follows:
*/
$email = "lvchenyang@live.cn";
$email =str_replace ("@", "(at)", $email);
echo "
". $email;
?>
0x07: Getting part of a string
Copy the Code code as follows:
<?php
/*
* Get part of a string
* The STRSTR () function returns the remainder of the string from the beginning of the first occurrence of a predefined string (including the occurrence string). The form is:
* String Strstr (string str,string occurrence[,bool Fefore_needle])
* Optional parameter Before_needle changes the behavior of the STRSTR () so that the function returns the first part of the string before it precedes it.
* The following example is to get the domain name in the right key, combined with the LTrim () function
*/
$url = "lvchenyang@live.cn";
echo "
". LTrim (Strstr ($url," @ ")," @ ");
?>
0X08: Part of a string that is based on the predefined cheap return
Copy the Code code as follows:
<?php
/*
* the substr () function returns the part of a string between start and start+length in the form of:
* String substr (String str,int start [, int length])
* If no optional arguments are specified, a string from start to end of STR is returned
* as shown below
*/
$str = "Lvchenyang";
echo "
". substr ($str, 2,4);
Output:chen
?>
0X09: Determining how often a string appears
Copy the Code code as follows:
<?php
/*
* Determine how often a string appears
* Substr_count () returns the number of occurrences of a string in another string. The form is:
* int Substr_count (string str,string substring [, int offset [, int length]])
* Optional parameter offset and length specify the string cheap (start trying to match the string from the cheap) and the string length (the length of the search from the cheap start)
* The following example determines the number of occurrences of each word in this sentence
*/
$talk =<<<>
I am acertain that we could dominate mindshare in this space with
Our new product, extablishing a true synergy Beteen the marketing
and product development teams. We ' ll own this space in thress months.
Talk
echo "
";
$sentencearray =explode ("", $talk);
foreach ($sentencearray as $item)
{
echo "The word $item appears (". Substr_count ($talk, $item). ") Times
";
}
?>
0X10: Replacing part of a string with another string
Copy the Code code as follows:
<?php
/*
* Replace a part of a string with another string
* The Substr_replace () function replaces a part of a string with another string, replacing it from the specified start position, knowing that the start+length position ends.
* In the form of:
* Stringsubstr_replace (String str,string repalcement,int start and length values.
* Replace the phone number in the middle 4 digits as shown below
*/
$phonenum = "15926841384";
echo "
". Substr_replace ($phonenum," * * * ", 3,4);
?>
http://www.bkjia.com/PHPjc/912669.html www.bkjia.com true http://www.bkjia.com/PHPjc/912669.html techarticle some string manipulation functions in PHP that can replace regular expression functions, PHP regular expression 0x01: Lexical analysis of strings based on predefined characters copy code code as follows: Ph ...