php函數preg_match查找URL,preg_match查找郵箱地址

高階用法示例:

  1. 使用 preg_match 函數來查找 URL:
<?php

$string = "https://www.example.com";
$pattern = "/(https?:\/\/)(www\.)?([a-z]+\.[a-z]+)/";
preg_match($pattern, $string, $matches);

print_r($matches);

// Output:
// Array
// (
//     [0] => https://www.example.com
//     [1] => https://
//     [2] => www.
//     [3] => example.com
// )

?>

  1. 使用 preg_match 函數來查找郵箱地址:
<?php

$string = "example@example.com";
$pattern = "/([a-z]+@[a-z]+\.[a-z]+)/";
preg_match($pattern, $string, $matches);

print_r($matches);

// Output:
// Array
// (
//     [0] => example@example.com
//     [1] => example@example.com
// )

?>

這些示例只是 preg_match 函數的一部分高級用法,實際上,該函數還具有很多其他功能,您可以根據需要進一步學習和了解。希望本篇文章對您有所幫助。

發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/php/3065.html

(0)
彬彬筆記彬彬筆記
上一篇 2023年2月19日
下一篇 2023年3月1日

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13