課程
/后端開發
/PHP
/PHP進階篇
<?php
//請修改變量p的正則表達式,使他能夠匹配str中的姓名
$p = '/[\w]+(\:[a-z]+\s[\w]+)/';
$str = "name:steven jobs";
preg_match($p, $str, $match);
echo $match[0]; //$match[0]和$match[1]區別在哪?
2016-12-11
源自:PHP進階篇 3-4
正在回答
$match是數組,$match[0]是其第一個元素,是取回的全匹配元素,$match[2]是其第二個元素,是()中指定要的元素。 你可以使用$str="other words name:steven jobs others thing"; 測試print_r($match); 的輸出,會更清晰:
$p = '/[\w]+\:([a-z]+\s[\w]+)/i';
$str="other words name:steven jobs others thing";
print_r($match);
?>
輸出:Array( ? ?[0] => name:steven jobs ? ?[1] => steven jobs)
web_東 提問者
是()中指定要的元素是什么意思?
舉報
輕松學習PHP中級課程,進行全面了解,用PHP快速開發網站程序
4 回答match[0和1]的區別是??
1 回答$match[1]其中換為0也是可以的啊
2 回答match的作用
1 回答為什么當$p = '/\w+\s\w+/';時,echo $match[0];才能輸出結果
1 回答為什么 會少一個s 在match[1]中
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-12-11
$match是數組,$match[0]是其第一個元素,是取回的全匹配元素,$match[2]是其第二個元素,是()中指定要的元素。 你可以使用$str="other words name:steven jobs others thing"; 測試print_r($match); 的輸出,會更清晰:
<?php
$p = '/[\w]+\:([a-z]+\s[\w]+)/i';
$str="other words name:steven jobs others thing";
preg_match($p, $str, $match);
print_r($match);
?>
輸出:
Array
(
? ?[0] => name:steven jobs
? ?[1] => steven jobs
)
2017-03-20
是()中指定要的元素是什么意思?