Home Backend Development PHP Tutorial PHP regular function

PHP regular function

Jul 28, 2016 am 08:27 AM
gt match pattern temp

php与javascript一样,正则的匹配内容通常用 / / 包起来,也可以使用其它非字母与数字起到同样作用,不能使用 \ \
$preg = ‘/ demo /’;


preg_match

<code><span><?php</span><span>$str</span> = <span>"abcdefgabcdbc"</span>;

    <span>$pattern</span> = <span>'/[da]bc/'</span>;
    <span>echo</span><span>'preg_match:'</span>.preg_match(<span>$pattern</span>, <span>$str</span>).<span>'<br>'</span>;

    <span>echo</span><span>'preg_match_all:'</span>.preg_match_all(<span>$pattern</span>, <span>$str</span>,<span>$matches</span>).<span>'<br>'</span>;

    <span>//preg_match_all匹配所有,并将其匹配项带入第三个参数 $matches</span>
    var_dump(<span>$matches</span>);
 <span>?></span><span>//</span>
preg_match:<span>1</span>
preg_match_all:<span>3</span><span>array</span>(<span>1</span>) { [<span>0</span>]=> <span>array</span>(<span>3</span>) { [<span>0</span>]=> string(<span>3</span>) <span>"abc"</span> [<span>1</span>]=> string(<span>3</span>) <span>"abc"</span> [<span>2</span>]=> string(<span>3</span>) <span>"dbc"</span> } }</code>
Copy after login

preg_replace

<code><span><span><?php</span><span>$str</span> = <span>'2014-11-25'</span>;
    <span>//使用()包括起来代表一个单元,可以与${n}对应使用,匹配的第几个内容会带入对应的${n}</span><span>$preg</span> = <span>'/(-)/'</span>;
    <span>$temp</span> = <span>'Year'</span>;
    <span>echo</span> preg_replace(<span>$preg</span>,<span>$temp</span>.<span>'${1}'</span>,<span>$str</span>);

    <span>echo</span><span>'<br>'</span>;

    <span>$preg</span> = <span>array</span>(
        <span>'/(\d+)/'</span>
    );
    <span>$temp</span> = <span>array</span>(
        <span>'temp[${1}]'</span>
    );
    <span>echo</span> preg_replace(<span>$preg</span>,<span>$temp</span>,<span>$str</span>);
<span>?></span></span>
//
2014Year-11Year-25
temp[2014]-temp[11]-temp[25]</code>
Copy after login

preg_split

<code><span><span><?php</span><span>$str</span> = <span>'1+2-3/4*5'</span>;
<span>$pattern</span> = <span>'#[+-/*]#'</span>;
<span>$res</span> = preg_split(<span>$pattern</span>,<span>$str</span>);
print_r(<span>$res</span>);
<span>//</span><span>Array</span> ( [<span>0</span>] => <span>1</span> [<span>1</span>] => <span>2</span> [<span>2</span>] => <span>3</span> [<span>3</span>] => <span>4</span> [<span>4</span>] => <span>5</span> )</span></span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 PHP之正则函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Teach you how to modify the temporary file location of Win7 Teach you how to modify the temporary file location of Win7 Jan 04, 2024 pm 11:25 PM

The temp folder is our temporary file storage location. The system will save temporary files to this folder. If there are too many temporary files, especially when the temp folder is on the system disk, it is likely to affect the system running speed. We can solve the problem by changing the temp location. Let’s take a look below. Tutorial on changing the location of win7temp 1. First, right-click "Computer" and open "Properties" 2. Click "Advanced System Settings" on the left 3. Click "Environment Variables" below 4. Select "temp" and click "Edit" 5. Then change Just change the "Variable Value" to the path that needs to be changed.

What does temp mean in computer? What does temp mean in computer? Sep 29, 2021 pm 04:39 PM

In computers, temp means "temporary folder", which contains many temporary files; its function is to temporarily save the user's work results in the application to prevent losses caused by accidents. Deleting temp files can free up hard disk storage space, but it will be slower to open the application for the first time after deletion.

How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] Apr 17, 2023 am 08:13 AM

Most of the devices, such as laptops and desktops, have been heavily used by young gamers and coders for a long time. The system sometimes hangs due to application overload. This forces users to shut down their systems. This mainly happens to players who install and play heavy games. When the system tries to boot after force shutdown, it throws an error on a black screen as shown below: Below are the warnings detected during this boot. These can be viewed in the settings on the event log page. Warning: Processor thermal trip. Press any key to continue. ..These types of warning messages are always thrown when the processor temperature of a desktop or laptop exceeds its threshold temperature. Listed below are the reasons why this happens on Windows systems. Many heavy applications are in

Internal error: Unable to create temporary directory [Resolved] Internal error: Unable to create temporary directory [Resolved] Apr 17, 2023 pm 03:04 PM

Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

What file is temp? What file is temp? Jan 05, 2021 am 10:43 AM

temp is a temporary folder. In the path "C:\Documents and Settings\Administrator\Local Settings\", many temporary files are placed here, including favorites, temporary files for browsing web pages, editing files, etc.

See all articles