Some string manipulation functions in PHP that can replace regular expression functions, php regular expression_PHP tutorial

WBOY
Release: 2016-07-13 10:13:56
Original
954 people have browsed it

Some string manipulation functions in PHP that can replace regular expression functions, php regular expressions

0x01: Lexical analysis of the string based on predefined characters

Copy code The code is as follows:

/*
* Regular expression functions can slow things down significantly when processing large amounts of information. These functions should only be used when you need to parse more complex strings using regular expressions. If you want to parse simple expressions, 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 character list. Its form is:
* string strtok(string str,string tokens)
* strtok() function, this function must be called continuously to completely perform lexical analysis on a string; each call to this function only performs lexical analysis on the next part of the string. However, the str parameter only needs to be specified once, because the function will keep track of the position in str until it has completely completed the lexical analysis of str, or the result str parameter has been specified.
* As shown in the example below:
*/
$info="lv chen yang|Hello:world&757104454@qq.com";
//Define delimiters, including (|)(:)( )(&)
$tokens="|:& ";
$tokened=strtok($info, $tokens);
while ($tokened)
{
echo "Element:$tokened
";
//Continuously call the strtok() function to complete the lexical analysis of the entire string
$tokened=strtok($tokens);
}
?>

0x02: Decompose string according to predefined delimiters

Copy code The code is as follows:

/*
* Decompose a string according to predefined delimiters: explode() function
* This function divides the string str into an array of substrings, in the form:
* array explode(string separator,string str [, int limit])
* The original string is split into different elements according to the string specified by separator. The number of elements can be limited with the optional limit parameter. explode()/sizeof() and strip_tags() can be combined to determine the total number of words in a given text block
* As shown below:
*/
$summary="
In the latest installment of the ongoing Developer.com PHP series.
I discuss the many improvements and additions 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, split() and spliti(). Therefore, you must use this function when you do not need to use regular expressions.
*/
?>

0x03: Convert array to string

Copy code The code is as follows:

/*
* Convert array to string
* The explode() function can convert a string into a corresponding array based on the delimiting characters, but the array can be converted into a string with specified delimiting characters as limits through the implode() function
* Its form is:
* 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 code The code is as follows:

/*
* Parse complex strings
* The strpos() function finds the first occurrence of substr in a string in a case-sensitive manner, in the form of
* int strpos(string str,string substr [,int offset])
* The optional parameter offset specifies the position to start the search. If substr is not in str, strpos() returns False. Optional arguments determine where strpos() begins its search.
* The following example will determine the timestamp of the first access to 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 position where $substr first appears in the log
$pos=strpos($log, $substr);
//Find the numerical position of the end of the line
$pos1=strpos($log,"n",$pos);
//Start of calculating timestamp
$pos=$pos+strlen($substr)+1;
//Retrieve timestamp
$timestamp=substr($log, $pos,$pos1-$pos);
echo "The file index.html was first accessed on: $timestamp
";
/*
* The usage of function stripos() and function strpos() are the same. The only difference is that stripos() is not case-sensitive.
*/
?>

0x05: Find the last occurrence of the string

Copy code The code is as follows:

/*
* Find the last occurrence of the string
* The strrpos() function searches for the last occurrence of the string and returns its position (numeric sequence number) in the form:
* int strrpos(string str,char substr [,offset])
* The optional parameter offset determines the starting search position of the strrpos() function. Added the hope of shortening lengthy news summaries,
* Cut off some parts of the summary and replace the cut off parts with ellipses. However, it is not simply about cutting the summary to the required length,
* You may want to cut in a user-friendly way 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 additions to
PHP object-oriented architecture. ";
if(strlen($summary)>$limit)
$summary=substr($summary, 0,strrpos(substr($summary, 0,$limit)," "))."...";
echo $summary;
?>

0x06: Replace all instances of a string with another string

Copy code The code is as follows:

/*
* Replace all instances of a string with another string
* The str_replace() function replaces all instances of a string with another string in a case-sensitive manner. Its form is:
* mixed str_replace(string occurrence, mixed replacement, mixed str [,int count])
* If occurrence is not found in str, str remains unchanged. If the optional parameter count is defined, only count occurrences in str will be replaced.
* This function is suitable for hiding electronic right-click addresses from programs that automatically obtain email addresses, as shown below:
*/
$email="lvchenyang@live.cn";
$email=str_replace("@", "(at)", $email);
echo "
".$email;
?>

0x07: Get part of the string

Copy code The code is as follows:

/*
* Get part of the string
* The strstr() function returns the remaining part of the string starting from the first occurrence of the predefined string (including the occurrence string). Its form is:
* string strstr(string str,string occurrence[,bool fefore_needle])
* The optional parameter before_needle will change the behavior of strstr() so that the function returns the part of the string before the first one.
* The following example is to obtain the domain name in the right click, combined with the ltrim() function
*/
$url="lvchenyang@live.cn";
echo "
".ltrim(strstr($url, "@"),"@");
?>

0x08: Return a part of the string according to the predefined value

Copy code The code is as follows:

/*
* The substr() function returns the part of the string between start and start+length, in the form:
* string substr(string str,int start [,int length])
* If no optional parameters are specified, return the string from start to the end of str
*As shown below
*/
$str="lvchenyang";
echo "
".substr($str, 2,4);
//output: chen
?>

0x09: Determine the frequency of string occurrence

Copy code The code is as follows:

/*
* Determine the frequency of string occurrence
* substr_count() returns the number of times a string appears in another string. Its form is:
* int substr_count(string str,string substring [,int offset [,int length]])
* The optional parameters offset and length specify the string offset (try to match the string starting from the offset) and the string length (the length of the search starting from the offset)
* The following example determines the number of times each word appears 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: Replace part of a string with another string

Copy code The code is as follows:

/*
* Replace part of a string with another string
* The substr_replace() function replaces part of a string with another string. The replacement starts from the specified start position and ends at the start+length position.
* Its form is:
* stringsubstr_replace(string str, string repalcement, int start and length values.
* As shown below, replace the middle 4 digits of the phone number
*/
$phonenum="15926841384";
echo "
".substr_replace($phonenum, "****", 3,4);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912669.htmlTechArticleSome string operation functions in PHP that can replace regular expression functions, php regular expression 0x01: according to predefined The characters of the string are lexically analyzed and copied. The code is as follows: ph...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template