<?php $pattern = '/^(?:(?:2[0-4][0-9].)|(?:25[0-5].)|(?:1[0-9][0-9].)|(?:[1-9][0-9].)|(?:[0-9].)){3}(?:(?:2[0-5][0-5])|(?:25[0-5])|(?:1[0-9][0-9])|(?:[1-9][0-9])|(?:[1-9]))$/'; //正则匹配ip地址 $ip = "254.21.1.198"; preg_match($pattern,$ip,$out); echo '<pre>'; print_r($out); echo '<pre>'; $ip = "254.21.1.198a"; var_dump(preg_match($pattern,$ip));