php 正则匹配字符串里的某个数据

php 正则匹配字符串里的某个数据,第1张

为什么不用json拿呢?
$obj = json_decode ( $json_str );
$verName = $obj['verName];
$verCode = $obj['verCode'];
算了,当我没说,根本不是标准json格式,都不造这个字串能用来作甚……

php自带一个函数similar_text,可以计算两个字符串的相似度,但是这个的准确性、速度不是很好。网上有很多其他的方法和现成的包,你可以搜索看看。下面简单列举一个类
class LCS {
var $str1;
var $str2;
var $c = array();
/返回串一和串二的最长公共子序列/
function getLCS($str1, $str2, $len1 = 0, $len2 = 0) {
$this->str1 = $str1;
$this->str2 = $str2;
if ($len1 == 0) $len1 = strlen($str1);
if ($len2 == 0) $len2 = strlen($str2);
$this->initC($len1, $len2);
return $this->printLCS($this->c, $len1 - 1, $len2 - 1);
}
/返回两个串的相似度/
function getSimilar($str1, $str2) {
$len1 = strlen($str1);
$len2 = strlen($str2);
$len = strlen($this->getLCS($str1, $str2, $len1, $len2));
return $len 2 / ($len1 + $len2);
}
function initC($len1, $len2) {
for ($i = 0; $i < $len1; $i++) $this->c[$i][0] = 0;
for ($j = 0; $j < $len2; $j++) $this->c[0][$j] = 0;
for ($i = 1; $i < $len1; $i++) {
for ($j = 1; $j < $len2; $j++) {
if ($this->str1[$i] == $this->str2[$j]) {
$this->c[$i][$j] = $this->c[$i - 1][$j - 1] + 1;
} else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) {
$this->c[$i][$j] = $this->c[$i - 1][$j];
} else {
$this->c[$i][$j] = $this->c[$i][$j - 1];
}
}
}
}
function printLCS($c, $i, $j) {
if ($i == 0 || $j == 0) {
if ($this->str1[$i] == $this->str2[$j]) return $this->str2[$j];
else return "";
}
if ($this->str1[$i] == $this->str2[$j]) {
return $this->printLCS($this->c, $i - 1, $j - 1)$this->str2[$j];
} else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) {
return $this->printLCS($this->c, $i - 1, $j);
} else {
return $this->printLCS($this->c, $i, $j - 1);
}
}
}

注:现在有很多座机都是8位了,除区号 的第一位是取值范围是3-9 这是国家规定的,
第二位 分省市、县取值不同。省会城市和大城市的区号三位,第一位选取“3、4、5、6、7、8、9”共七个数字
第二位选取单数“1、3、5、7、9”共五个数字,第三位随意。
各县城及小城市的区号四位,第一位选取“3、4、5、6、7、8、9”共七个数字,
第二位选取双数“2、4、6、8、0”共五个数字,第三、四位随意。
//$str = "028-8711934"; 大陆7位座机 3位区号
//$str = "0827-87119345"; 大陆8位座机 3位区号
//$str = "0827-87119345"; 大陆8位座机 4位区号
//$str = "00852-87119345"; 香港8位座机
//$str = "00852-8711934"; 香港7位座机
//手机 13、14、15、18 开头的

这里就不多列举了。
$mode = "/(^1[3|4|5|8][0-9]{9}$)|(^0[1-9]{2,4}[-][3-9]{1}[0-9]{6,7}$)/";
if(preg_match($mode, $str)){
echo "匹配";
}
else{
echo "不匹配";
}上面那个正则有到问题 取值范围差了一个
下面这个 ok。
$mode = "/(^1[3|4|5|8][0-9]{9}$)|(^0[0-9]{2,4}[-][3-9]{1}[0-9]{6,7}$)/";

第一个问题:
preg_match("/<div>((:(!<div))+)<\/div>/", $content, $match);
echo $match[1];
第二个问题,LZ是不是想匹配出b、c中先出现的一处中的内容?这样的话:
$content = '
<div class="b">内容</div>
<div class="c">内容</div>';
preg_match("/<div\sclass=\"[bc]\">(+)<\/div>/", $content, $match);
echo $match[1];

<php
$str = '<tr>
    <td rowspan="2" class="col">20150630 </td>
    <td class="col">AAAA </td>
    <td class="col">BBB</td>
    <td class="col">CCC</td>
    <td class="col">DDD</td>
  </tr>';
preg_match_all('/<td[^>]>()<\/td>/is', $str , $matched);
print_r($matched[1]);
exit;


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/12907368.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-28
下一篇2025-08-28

发表评论

登录后才能评论

评论列表(0条)

    保存