php结合正则获取字符串中数字
这篇文章主要给大家汇总介绍了php结合正则获取字符串中数字的几种方法,十分的简单实用,有需要的小伙伴可以参考下。
php结合正则获取字符串中数字
Array ( [0] => 0 [1] => 202 [2] => 90 [3] => 30 ) ) //第二种 Array ( [0] => Array ( [0] => 0 [1] => 2 [2] => 0 [3] => 2 [4] => 9 [5] => 0 [6] => 3 [7] => 0 ) )
php 获取指定字符串的数字 $a="时代发123生的12345678发生的"; 给一个函数能抓取到$a中的数字12345678,如果没有,则返回0
要写好函数,
1.只匹配长度位8的数字
2.如果找到则返回改数字,如果没有则返回0
function a($str){ return preg_match('/([0-9]{8})/',$str,$a) ? $a[1] : 0; }
在网上搜索到这个测试一下发现还可以,,但是如果字符长度不d{4} 不定长就不行了
function findNum($str=''){ if(empty($str)){return '';} $reg='/(\d{4}(\.\d+)?)/is';//匹配数字的正则表达式 preg_match_all($reg,$str,$result); if(is_array($result)&&!empty($result)&&!empty($result[1])&&!empty($result[1][0])){ return $result[1][0]; } return ''; }
后来又发现知道中有一个
$str=trim($str); if (preg_match('|(\d+)|',$str,$r)) return $r[1];
发现这个可以获取任意连续长度的数字了,当然在网上还看到很多,但有一个可以了所以就没有再测试了。
以上所述就是本文的全部内容了,希望大家能够喜欢。

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

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Validator can be created by adding the following two lines in the controller.
