


Detailed explanation of strings and regular expressions in php, detailed explanation of php regular expressions_PHP tutorial
Detailed explanation of strings and regular expressions in php, detailed explanation of php regular expressions
1. Characteristics of string type
1. PHP is a weakly typed language, and other data types can generally be directly applied to string function operations.
echo substr("123456",2,4); //输出345<br>echo substr(123456,2,4); //输出345<br>echo hello; //先查找hello常量,若没找到,将hello看做字符串使用<br>?>
2. Strings can be used as "arrays", which are collections of characters.
$str = "www.jb51.net";<br>echo $str[0];<br>echo $str[1];<br>echo $str[2];<br>?>
But strings are not real arrays, and array functions cannot be used. For example, count($str) will not return the length of the string. The PHP engine cannot distinguish between characters and arrays, causing ambiguity. Since PHP 4, curly braces have been used instead of square brackets.
//为保证向后兼容,方括号仍然可以使用<br>$str = www.jb51.net;<br>echo $str{0};<br>echo $str{1};<br>echo $str{2};<br>?>
3. Double quote variable analysis
In PHP, when a string is defined with double quotes or delimiters, the variables in it will be parsed.
$arr = array('name' => "dwqs",'add' => "www.ido321.com");<br>echo "$arr[name]"; //可以解析,但是在方括号中不能使用引号<br>//echo "$arr['name']"; 错误<br>echo "{$arr['name']}"; //可以解析,用花括号包含元素,name不带引号也是可以的<br>//假设存在对象$square<br>echo "$square->width"; //可以解析<br>echo "$square->width00 cent"; //不可以解析,用花括号解决<br>echo "{$square->width}width00 cent"; //可以解析<br>?>
2. String output function
3. Commonly used string format functions
PS: Most of PHP’s string processing functions do not modify the source string, but return a new string
4. Regular expressions
Regular expression describes a string matching pattern, through which strings are matched, searched, replaced and separated in specific functions. It consists of three parts: atoms, metacharacters and pattern modifiers. text mode.
In PHP, there are two sets of regular processing function libraries: PCRE and POSIX. The former is named with preg_ prefix and is compatible with Perl; the latter is named with ereg_ prefix. The functions of the two are similar, but the efficiency of PCRE is slightly higher.
Regular expression processing function compatible with Perl language:
1. Grammar
1.1 Delimiter: When using a pattern in a Perl-compatible regular function, you must add a delimiter to the pattern. Any character except letters, numbers and backslash () can be used as a delimiter
//以下正则合法<br>echo $m1 = '/echo $m2 = '|(\d{3})-\d|Sm';<br>echo $m3 = '!^(?i)php[34]!';<br>echo $m4 = '{^\s+(\s+)?$}';<br>?>
1.2 Atoms: Atoms include ordinary characters, such as letters and numbers; non-printing characters, such as spaces and carriage returns; special characters and metacharacters, such as quotation marks, *, +, etc., which must be escaped with ""; self Define atomic tables, such as [apj], [a-z]; general character types, such as d, D.
//下面二者等价,匹配e-mail<br>$mail1 = '/^[0-9a-zA-Z]+@[0-9a-zA-Z]+(\.[0-9a-zA-Z]+){0,3}$/';<br>$mail2 = '/^\w+@\w+(\.\w+){0,3}$/';<br>?>
1.3 Metacharacters: Characters with special meanings used to build regular expressions. Perl can use various metacharacters to search for matches, such as *, +, ? .Common metacharacters are as follows
1.4 Pattern modifier: Used in addition to regular delimiters to extend regular functions in matching, replacement, etc.
2. Regular expression function compatible with Perl
2.1 preg_match(string pattern,string subject[,array matches]): used to search and match strings. Parameter description:
pattern is a regular pattern, subject is a string that needs to be processed, optional matches are used to save the matching results of each sub-pattern of pattern, matches[0] saves the overall content matching pattern, matches[1] saves The matched content in the first parentheses of pattern, and so on.
header("content-type:text/html;charset=utf8");<br>$pattern = '/(http):\/\/(www)\.([^\.\/]+)\.(com|net|org)/i';<br>$subject = "我的博客:http://www.ido321.com";<br>if(preg_match($pattern, $subject,$matches)){<br>echo "搜索的URL是:".$matches[0]."<br>"; //数组第1个元素保存整个匹配结果<br>echo "URL中的协议是:".$matches[1]."<br>";//数组第2个元素保存第1个字表达式<br>echo "URL中的主机是:".$matches[2]."<br>";//数组第3个元素保存第2个字表达式<br>echo "URL中的域名是:".$matches[3]."<br>";//数组第4个元素保存第3个字表达式<br>echo "URL中的顶域是:".$matches[4]."<br>";//数组第5个元素保存第4个字表达式<br>}<br>?>
Results
preg_match_all() is similar to the preg_match() function, except that the former will match until the end of the string, while the latter will stop matching after the first match.
2.2 preg_grep(string pattern, array iput): Match the elements in the array and return the array unit that matches the regular pattern. Parameter description:
pattern is a regular pattern, and input is an array that needs to be matched.
$arr = array('Linux RedHat9.0','Apache2.2.9','MySQL5.0.51','PHP5.2.6','LAMP','100');<br>$version = preg_grep('/^[a-zA-Z]+(\d|\.)+$/',$arr);<br>//输出:Array([1]=>Apache2.2.9 [2]=>MySQL5.0.51 [3]=>PHP5.2.6)<br>print_r($version); <br>?>
2.3 preg_replace(mixed pattern, mixed replacement, mixed subject[,int limit]): String replacement. Description:
This function will search for a match with pattern in the subject and replace it with replacement. Limit is used to limit the number of matches, that is, the number of substitutions.
$pattern = '/]*?/is';<br>$text = '这个文本有<b>粗体</b>和<u>带有下划线</u>以及<i>斜体</i>';<br>echo preg_replace($pattern,"",$text); //将所有HTML标记替换为空<br>echo preg_replace($pattern,"",$text,2); //值替换前2个HTML标记<br>?>
2.4 preg_split(string pattern,string subject[,int limit[,int flags]]): Split the string. Description:
The function returns an array. The array elements contain strings in the subject divided by boundaries that match pattern. For the meaning of limit, see 2.3. For the meaning of flags, please refer to the documentation.
//按任数量的空格分割字符串<br>$kerwords = preg_split("/[\s,]+/","hypertext language,programming");<br>//输出:Array([0]=>hypertext [1]=>language,[2[=>programming)<br>print_r($kerwords);<br>?>
$str='{"a":1234567890,"b":"u","birthday":"2000-01-01","gender":"1","location ":"123456","login_ip":"123.123.123.123","login_time":1234567890,"id":"1234567","sign":"0bcbdea54d1f2c3c75b058eb5d2ae124"}';
$user_id="";
if (preg_match_all('|"id":"(\S+?)"|', $str, $reg))
{
$user_id=$reg[1][0];
}
echo "UserID is : ".$user_id;
?>
Output result:
UserID is : 1234567
You can use regular expressions or php directly. I feel that php is more convenient:
$str = str_replace("","

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft