Hongmu Notes
Home Language Notes PHP: Matches numbers within a title – numbers with two or more digits.
Language Notes PHP

PHP: Matches numbers within a title – numbers with two or more digits.

PHP: Matches numbers within a title – numbers with two or more digits.

You can use PHP's regular expression functionality to match numbers with two or more digits in a title. Here is a simple example:

$title = "1341响应式照明灯饰壁灯企业网站模板";
$pattern = "/\d{2,}/"; // 匹配两位数及以上的数字
preg_match($pattern, $title, $matches);
echo $matches[0]; // 输出 "1341"

In this example, we use the `preg_match` function, which takes three parameters: a regular expression pattern, an input string, and a variable name for storing the matching result. The regular expression pattern ` "/\d{2,}/" ` matches two or more digits. \d Matches any numeric character.{2,} Matches two or more numeric characters.

By using the `preg_match` function and storing the matching results in $matches In an array, we can easily extract two-digit or larger numbers from a title. In this example,$matches[0] Stores the first matching string – namely, "1341" from the title. If the title contains multiple matches, this function can be used. $matches[1]$matches[2] Wait until you can obtain them.

Note that this example only matches the first two-digit or longer number in the title. If you need to match all such numbers in the title, you can use the `preg_match_all` function. For example:

$title = "1341响应式照明灯饰壁灯企业网站模板";
$pattern = "/\d{2,}/"; // 匹配两位数及以上的数字
preg_match_all($pattern, $title, $matches);
print_r($matches[0]); // 输出 Array([0] => 1341)

In this example, we use the `preg_match_all` function; this function is similar to the `preg_match` function, but it can match all occurrences. In this example,$matches[0] Contains all matched strings, i.e., "1341". Since there is only one match in this example, the array contains a single element. If there are multiple matches in the title, the array will contain multiple elements, each representing one match.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Responsive exquisite bouquet flower gift website template 0990

Responsive exquisite bouquet flower gift website template 0990 Practical Collection Yiyou template

An eyoucms responsive website template for exquisite bouquets and flower gifts. The design style is fresh and beautiful, and can display flower bouquet products, gift designs, brand stories and delivery services. It helps flower brands display their products online and attract individual and corporate customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Summary of common problems in Yiyou CMS installation...
👁 61
Marketing-oriented Fire Extinguisher and Firefighting Equipment Website Template 0174

Marketing-oriented Fire Extinguisher and Firefighting Equipment Website Template 0174 Practical Collection Yiyou template

An eYouCMS website template designed for marketing-oriented fire extinguisher, firefighting equipment, and apparatus manufacturers. Its eye-catching and professional design enables effective presentation of firefighting products, safety equipment, technical specifications, and project case studies. This template helps firefighting equipment companies showcase their products online, attracting both corporate and government procurement opportunities. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Installation Issues with eYouCMS...
👁 29
(PC + WAP) Catering & Milk Tea Franchise Website – pbootCMS Template; Food & Snack Franchise Website Source Code Download – 0555

(PC + WAP) Catering & Milk Tea Franchise Website – pbootCMS Template; Food & Snack Franchise Website Source Code Download – 0555 Practical Collection pbootcms Template

A PbootCMS website template designed for the catering, milk tea franchise and food & snack industries, supporting both PC and WAP devices. Its fashionable and vibrant design effectively appeals to young entrepreneurs. The template includes built-in modules for brand storytelling, franchise policies, and online consultation – making it a powerful tool for fast-track franchise recruitment for restaurant chains. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extract...
👁 53
How to upload files using Qiniu Cloud Object Storage with PHP? How to compress images during file upload?

How to upload files using Qiniu Cloud Object Storage with PHP? How to compress images during file upload? Summary of pitfalls

Recently, I came across an image-sharing platform and, out of necessity, decided to use Object Storage. After reviewing the official documentation, I finally managed to implement the functionality I needed – so, here's a summary! How do you upload files to Qiniu Cloud Object Storage? First, include the official SDK file in your project. Then, in PHP, simply write the code; due to the confidentiality requirements of my business, I will only share the code for the file-upload portion! <?php require'./vendor/a...
👁 173
Responsive maternal and infant products franchise website template – 0991

Responsive maternal and infant products franchise website template – 0991 Practical Collection Yiyou template

This eyouCMS responsive template is ideal for the maternal and infant products franchise industry. Its warm yet professional design effectively showcases product offerings, franchise policies, brand stories, and store imagery, helping maternal and infant brands attract franchisees and consumers online. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 59
Responsive bilingual (Chinese/English) building materials exhibition and sales website template – 0175

Responsive bilingual (Chinese/English) building materials exhibition and sales website template – 0175 Practical Collection Yiyou template

This EyouCMS responsive bilingual (Chinese/English) template is ideal for the building materials exhibition and sales industry. Its international, professional design style enables effective presentation of building materials products, export advantages, project cases, and brand image. The multilingual support facilitates brand promotion for building materials enterprises in global markets. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 61