Home > php教程 > php手册 > PHP提取字符串中的数字

PHP提取字符串中的数字

WBOY
Release: 2016-06-07 11:43:11
Original
1166 people have browsed it

PHP提取字符串中的数字,摘自网络。
第一种方法,使用正则表达式:function findNum($str=''){<br>         $str=trim($str);<br>         if(empty($str)){return '';}<br>         $reg='/(\d{3}(\.\d+)?)/is';//匹配数字的正则表达式<br>         preg_match_all($reg,$str,$result); if(is_array($result)&&!empty($result)&&!empty($result[1])&&!empty($result[1][0])){<br>             return $result[1][0];<br>         }<br>         return '';<br>     }第二种方法,使用in_array方法:function findNum($str=''){<br>         $str=trim($str);<br>         if(empty($str)){return '';}<br>         $temp=array('1','2','3','4','5','6','7','8','9','0');<br>         $result='';<br>         for($i=0;$i<strlen></strlen>             if(in_array($str[$i],$temp)){<br>                 $result.=$str[$i];<br>             }<br>         }<br>         return $result;<br>     }第三种方法,使用is_numeric函数:function findNum($str=''){<br>         $str=trim($str);<br>         if(empty($str)){return '';}<br>         $result='';<br>         for($i=0;$i<strlen></strlen>             if(is_numeric($str[$i])){<br>                 $result.=$str[$i];<br>             }<br>         }<br>         return $result;<br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template