


Some examples of regular functions related to PHP and Python_PHP tutorial
When we are doing string processing, if the string processing function cannot achieve what we want, we use regular expressions to help us achieve it.
The general situations in which regular expressions are used include: matching, search, segmentation, search and replace. Below we will implement these situations in PHP and Python respectively, and make a comparison. PHP regular adopts: PCRE style.#1 Match Math (and get the result)(Note that here you need to get the matching result, which is different from not getting the result)
PHP:
#Output: string 'P' (length=1)
Note: preg_match() is the same as match in python. It will stop searching after the first match. Preg_match_all() is different from this, it will search the subject until it reaches the end. Actually, regular expressions in PHP can also look like this:
preg_match('/(w+)/',$strs,$m);
#2 SearchSearch Python:patt = re.compile(r'(w+)',re.I) print patt.search(strs).group(1) #Output P
The search method is the same. If it is found, it will be returned immediately. Otherwise, it will be searched until the end of the string. In PHP, you can use preg_match(_all) to achieve this. PHP: Same as above #3 Match segmentation Python:patt = re.compile(r'w+',re.I) for i in patt.split(strs): #Note that unicode object output is used here print unicode(i,'utf-8') #The above output '''I love you, do you know? Hahaha'''
You can use preg_split() in PHP to achieve this PHP:
/**Output:
<i><b>array</b> 0 => string '我爱' <i>(length=6)</i> 1 => string '你' <i>(length=3)</i> 2 => string '你' <i>(length=3)</i> 3 => string '知' <i>(length=3)</i> 4 => string '吗' <i>(length=3)</i> 5 => string '?' <i>(length=3)</i> 6 => string '哈哈' <i>(length=6)</i> 7 => string '哈' <i>(length=3)</i></i>
#4 Search to find all results (ALL) Python:
print patt.findall(strs) #Output ['P', 'y', 't', 'h', 'o', 'n', 'fe']
Preg_match_all() can be used in PHP to achieve PHP:
/**
<i><i><b>array</b> 0 => <b>array</b> 0 => string 'P' <i>(length=1)</i> 1 => string 'y' <i>(length=1)</i> 2 => string 't' <i>(length=1)</i> 3 => string 'h' <i>(length=1)</i> 4 => string 'o' <i>(length=1)</i> 5 => string 'n' <i>(length=1)</i> 6 => string 'fe' <i>(length=2)</i> 1 => <b>array</b> 0 => string 'P' <i>(length=1)</i> 1 => string 'y' <i>(length=1)</i> 2 => string 't' <i>(length=1)</i> 3 => string 'h' <i>(length=1)</i> 4 => string 'o' <i>(length=1)</i> 5 => string 'n' <i>(length=1)</i> 6 => string 'fe' <i>(length=2) **/ </i></i></i>
for i in patt.finditer(strs): print i.group() #The above output ''' P y t h on fe '''
这和PHP中的preg_filter()有所不同,preg_filter()与preg_replace()都是执行一个正则表达式的搜索和替换。在python中正则方法中,用于查找替换的是:sub()与subn()。 需要注意的是sub()返回的一个新字符串,不是作用在原对象上。 subn()返回的是一个以“新字符串和替换的次数”组成的元组,也没有作用到原对象上。 #替换三次 print patt.sub('99',strs,3) #输出 '我爱99你99你99知h吗o?n哈哈fe哈' print patt.subn('99',strs) #输出:是一个元组('我爱99你99你99知99吗99?99哈哈99哈',7) 替换与引用#这里批量替换文章中的图片的路径(old_c 是文章的内容)
img_dir = 'test'
img_patt = re.compile('src=".*?/(\w+\.\w+)"')
new_c = img_patt.sub(r'src="./%s/\1"'%img_dir,old_c) PHP:
#这里批量替换文章中的图片的路径(old_c 是文章的内容)
img_dir = 'test' img_patt = re.compile('src=".*?/(\w+\.\w+)"') new_c =img_patt.sub(r'src="./%s/\1"'%img_dir,old_c) #输出:
<i><i><i><font><b><b><font>string '我爱999你999你999知999吗999?999哈哈999哈' <i>(length=51) </i></font></b></b></font></i></i></i>


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

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.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

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.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

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.
