


Strings and regular expressions in php, php regular expressions_PHP tutorial
Strings and regular expressions in php, php regular expressions
1. Characteristics of string types
1. PHP is a weakly typed language, and other data types can generally be directly applied to string function operations.
1://输出345
//输出345
//先查找hello常量,若没找到,将hello看做字符串使用
2、字符串可以作为“数组”,是字符的集合。
1:
<span id="lnum3"> 3:</span><span> <span>echo</span> $str[0];</span>
<span id="lnum5"> 5:</span><span> <span>echo</span> $str[2];</span>
但是字符串不是真的数组,不能使用数组的函数.如count($str)不会返回字符串长度。PHP引擎无法区分字符和数组,产生二义性。自PHP4起,已经用花括号替代方括号。
1:
<span id="lnum3"> 3:</span><span> $str = <span>"www.ido321.com"</span>;</span>
<span id="lnum5"> 5:</span><span> <span>echo</span> $str{1};</span>
<span id="lnum7"> 7:</span><span> ?></span>
3. Double quote variable analysis
In PHP, when a string is defined with double quotes or delimiters, the variables in it will be parsed.
1:"dwqs",<span>'add'</span> => <span>"www.ido321.com"</span>);
//可以解析,但是在方括号中不能使用引号
<span id="lnum5"> 5:</span><span> <span>echo</span> <span>"{$arr['name']}"</span>; <span>//可以解析,用花括号包含元素,name不带引号也是可以的</span></span>
<span id="lnum7"> 7:</span><span> <span>//假设存在对象$square</span></span>
; <span>//可以解析</span>
; <span>//不可以解析,用花括号解决</span>
; <span>//可以解析</span>
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 atoms, metacharacters and pattern modifiers. Three-part text pattern.
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:
<span><img src="/static/imghwm/default1.png" data-src="http://img.blog.csdn.net/20140916122411809?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTA0Mzg0Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast?x-oss-process=image/resize,p_40" class="lazy" alt="" /></span>
1. Grammar
1.1 Delimiter: When using a pattern in a regular function compatible with Perl, you must add a delimiter to the pattern. Any character except letters, numbers and backslash () can be used as a delimiter
1:<span id="lnum3"> 3:</span><span> <span>echo</span> $m1 = <span>'/<\/\w+/'</span>;</span>
<span id="lnum5"> 5:</span><span> <span>echo</span> $m3 = <span>'!^(?i)php[34]!'</span>;</span>
<span id="lnum7"> 7:</span><span> ?></span>
1.2 Atoms: Atoms include normal characters, such as letters and numbers; non-printing characters, such as spaces and carriage returns; special characters and metacharacters , such as quotation marks, *, +, etc., must be escaped with ""; Customized atom table, such as [apj], [a-z]; Universal character type, Such as d, D.
1:<span id="lnum3"> 3:</span><span> $mail1 = <span>'/^[0-9a-zA-Z]+@[0-9a-zA-Z]+(\.[0-9a-zA-Z]+){0,3}$/'</span>;</span>
<span id="lnum5"> 5:</span><span> ?></span>
1.3 Metacharacters: Characters with special meanings used to construct 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 that matches pattern, matches[ 1] Saves the matched content in the first parentheses of pattern, and so on.
1:);
<span id="lnum4"> 4:</span><span> $subject = <span>"我的博客:http://www.ido321.com"</span>;</span>
<span id="lnum6"> 6:</span><span> <span>echo</span> <span>"搜索的URL是:"</span>.$matches[0].<span>"<br/>"</span>; <span>//数组第1个元素保存整个匹配结果</span></span>
;<span>//数组第2个元素保存第1个字表达式</span>
;<span>//数组第3个元素保存第2个字表达式</span>
;<span>//数组第4个元素保存第3个字表达式</span>
;<span>//数组第5个元素保存第4个字表达式</span>
<span id="lnum12"> 12:</span><span> ?></span>
Results
preg_match_all()与preg_match()函数类似,不同的是前者会一直匹配到字符串末尾,后者在第一次匹配后就停止匹配。
2.2 preg_grep(string pattern,array iput):匹配数组中的元素,返回与正则匹配的数组单元。参数说明:
pattern是正则,input是需要匹配的数组。
1:<span id="lnum3"> 3:</span><span> $version = preg_grep(<span>'/^[a-zA-Z]+(\d|\.)+$/'</span>,$arr);</span>
<span id="lnum5"> 5:</span><span> <span>//输出:Array([1]=>Apache2.2.9 [2]=>MySQL5.0.51 [3]=>PHP5.2.6)</span></span>
<span id="lnum7"> 7:</span><span> ?></span>
2.3 preg_replace(mixed pattern,mixed replacement,mixed subject[,int limit]):字符串替换。说明:
该函数会在subject中搜索与pattern的匹配项,并用replacement替换。limit用于限制匹配的次数,即替换的次数。
1:<span id="lnum3"> 3:</span><span> $text = <span>'这个文本有<b>粗体</b>和<u>带有下划线</u>以及<i>斜体</i>'</span>;</span>
//将所有HTML标记替换为空
//值替换前2个HTML标记
2.4 preg_split(string pattern,string subject[,int limit[,int flags]]):对字符串进行分割。说明:
函数返回一个数组。数组元素包含subject中与pattern匹配作为边界所分割的字符串,limit含义见2.3,flags含义请参考文档。
<span id="lnum2"> 2:</span><span> <span>//按任数量的空格分割字符串</span></span>
<span id="lnum4"> 4:</span><span> </span>
<span id="lnum6"> 6:</span><span> print_r($kerwords);</span>
来源:http://www.ido321.com/612.html
$text = '
。。。。。。
test';
preg_match_all('/print_r($m);
$s="soproxy.appspot.com/...opular";
preg_match('/v=(.*?)&/',$s,$matched);
echo($matched[1]);

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)