在PHP中查找模式


PHP find patern in php

我有一个字符串像

$str="with a little love this home can be hud case # 351-377554. property sold as-is. information from sources deemed reliable but not guaranteed. buyer to verify all information. streamline k eligible.";

这里我需要得到# 351-377554

使用正则表达式。

<?php
$str="with a little love this home can be hud case # 351-377554. property sold as-is. information from sources deemed reliable but not guaranteed. buyer to verify all information. streamline k eligible.";
preg_match("/# [0-9]{3}-[0-9]+/", $str, $matches);
print_r( $matches );
    <
  • 生活预览/gh>正则表达式测试

İf数字总是以相同的格式(即以#开始,以。结束),那么你可以使用:

$exploded1 = explode('#', $str);
$exploded2 = explode('.', $exploded1[1]);
echo $exploded2[0]; //will echo number

你可以在这里检查爆炸功能