Table of Contents
Some string manipulation functions in PHP that can replace regular expression functions, php regular expressions
Home Backend Development PHP Tutorial Some string manipulation functions in PHP that can replace regular expression functions, php regular expression_PHP tutorial

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

Jul 13, 2016 am 10:13 AM
mysql php String operations

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:

<?php
/*
* 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<br/>";
//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:

<?php
/*
* 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
<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, 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:

<?php
/*
* 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:

<?php
/*
* 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=<<<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 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<br/>";
/*
* 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:

<?php
/*
* 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
<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: Replace all instances of a string with another string

Copy code The code is as follows:

<?php
/*
* 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 "<br/>".$email;
?>

0x07: Get part of the string

Copy code The code is as follows:

<?php
/*
* 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 "<br/>".ltrim(strstr($url, "@"),"@");
?>

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

Copy code The code is as follows:

<?php
/*
* 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 "<br/>".substr($str, 2,4);
//output: chen
?>

0x09: Determine the frequency of string occurrence

Copy code The code is as follows:

<?php
/*
* 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=<<<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 "<br/>";
$sentencearray=explode(" ", $talk);
foreach ($sentencearray as $item)
{
echo "The word <strong>$item</strong> appears(".substr_count($talk, $item).")times<br/>";
}
?>

0x10: Replace part of a string with another string

Copy code The code is as follows:

<?php
/*
* 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 "<br/>".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...
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles