Home > Backend Development > PHP Tutorial > PHP正则的小疑点

PHP正则的小疑点

WBOY
Release: 2016-06-13 12:03:22
Original
945 people have browsed it

PHP正则的小问题
    $url = "http://detail.tmall.com/item.htm?id=39583642975&ali_trackid=2:mm_17615479_5728618_19968131,00236231:1406685924_6k5_1303432088&spm=a230z.1.5634029.1.Ch9V0b";

    $preg = "/,([0-9]+):/is";
    preg_match($preg,$url,$a);
    print_r($a);

为什么我用正则的时候用 ^    $  从来都没有成功过,所以我从来都不用这两个符号,现在有点受不了的感觉,就比如上面的句是可以匹配的,但
$preg = "/^,([0-9]+):/is"; 这个肯定不行
$preg = "/^,([0-9]+):$/is"; 这个肯定不行

求高手解释一下为毛不行?
------解决方案--------------------

引用:
你这等于没说....


你的字符串是http开头而不是半角逗号开头,显然匹配不上,我还要说什么...
------解决方案--------------------
^     匹配【输入】字符串的开始位置
$    匹配【输入】字符串的结束位置

$preg = "/^,([0-9]+):/i";//只能匹配,00236231:1406685924_6k5_1303432088&spm=a230z.1.5634029.1.Ch9V0b(以你字符串为例)
$preg = "/^,([0-9]+):$/i";//只能匹配,00236231:(以你字符串为例)
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template