PHP regular Match URL

Source: Internet
Author: User
Tags preg printable characters

Amazon Goods URL;

$url _array = [

' Https://www.amazon.com/dp/B073CNGGWR?aaxitk=Wthh1MTV7dbwcbCGO506iw&pd_rd_i=B073CNGGWR&pf_rd_m= Atvpdkikx0der&pf_rd_p=3930100107420870094&pf_rd_s=desktop-sx-top-slot&pf_rd_t=301&pf_rd_i=tx &hsa_cr_id=8370788670301&sb-ci-n=asinimage&sb-ci-v=https%3a%2f%2fimages-na.ssl-images-amazon.com% 2fimages%2fi%2f41pnbzhboel.jpg&sb-ci-a=b073cnggwr ',
' Https://www.amazon.com/Mysticbags-Waterproof-Backpack-Primary-Students/dp/B071D37XQD/ref=sr_1_1_sspa?s= apparel&ie=utf8&qid=1534865412&sr=1-1-spons&nodeid=7141123011&psd=1&keywords=book& Psc=1 ',

];

Gets the string from https://to/dp/through the regular;

It is implemented by the following code:
$preg = '/https:\/\/www. [^\s*]*\/dp\/\w* (\/|\?) /I ';

foreach ($url _array as $index = $v)
{
Preg_match_all ($preg, $v, $matche);
$url _result_data[] = $matche [0][0];
}

DD ($url _result_data);

Echo:
Array:2 [ 1 => "https://www.amazon.com/dp/B073CNGGWR?" 2 => "https://www.amazon.com/Mysticbags-Waterproof-Backpack-Primary-Students/dp/B071D37XQD/"]

Regular related attention knowledge points:
\ for Escape
\s any one character except for whitespace characters
* Match 0~n times the atoms in front of it,
\w matches any number, letter, underscore equivalent to [0-9a-za-z_]
\w Matching non-\w
| Match two or more branch selections
() is used as a pattern unit, which makes large atoms of multiple atoms, and is used as a separate unit. Eg:/(very) *good/can match good, very good, very very very good, or very very ... good,
. Matches any one of the characters in the target, including non-printable characters, and matches any one character except the line break. Eg:/a.b/matches a string of any character between A and B for example: AXB,ATB,ACB,AZB, etc.



PHP regular Match URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.