Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial PHP正则的小问题

PHP正则的小问题

Jun 23, 2016 pm 01:52 PM
php regular

    $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:(以你字符串为例)



你这等于没说....



你的字符串是http开头而不是半角逗号开头,显然匹配不上,我还要说什么...



意思就是用^这个的时候必须是在字符串的最前面那个字符吗?$这个必须是最后的那个?   要从中间取一段的话就不能用了是吗?

^     匹配【输入】字符串的开始位置
$    匹配【输入】字符串的结束位置

$preg = "/^,([0-9]+):/i";//只能匹配,00236231:1406685924_6k5_1303432088&spm=a230z.1.5634029.1.Ch9V0b(以你字符串为例)
$preg = "/^,([0-9]+):$/i";//只能匹配,00236231:(以你字符串为例)




明白了  3Q
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles