PHP的ereg函數(shù)如何使用

PHP
小樊
89
2024-08-08 07:30:48

ereg函數(shù)在PHP中已經(jīng)廢棄,不再推薦使用。相反,建議使用preg_match函數(shù)來(lái)進(jìn)行正則表達(dá)式的匹配。下面是ereg函數(shù)的用法示例:

$pattern = "[0-9]+";
$string = "Hello 123 World";

if (ereg($pattern, $string)) {
    echo "String contains numbers";
} else {
    echo "String does not contain numbers";
}

使用preg_match函數(shù)的替代方法如下:

$pattern = "/[0-9]+/";
$string = "Hello 123 World";

if (preg_match($pattern, $string)) {
    echo "String contains numbers";
} else {
    echo "String does not contain numbers";
}

上述示例中,preg_match函數(shù)使用了斜杠“/”來(lái)包裹正則表達(dá)式的模式,并且返回一個(gè)整型值來(lái)表示匹配是否成功。

0