Related issues with character matching

Source: Internet
Author: User
Problem with character matching
Can you tell me how to match the following characters in a bunch of source code
IMG id= "main-image" src= "Http://ecx.images-mazon.com/images/I/41Auzc7RBPL._SY300_.jpg"

I mainly want to get to the address of the picture, where 41auzc7rbpl._sy300_ (the file name of the image is randomly generated, each time is different),
How can I get to http://ecx.images-mazon.com/images/I/41Auzc7RBPL._SY300_.jpg

Share to:


------Solution--------------------
It is most convenient to use regular processing:
$str = ' How do I match the following characters in a bunch of source code
IMG id= "main-image" src= "http://ecx.images-mazon.com/images/I/41Auzc7RBPL._SY300_.jpg";

$p = ' #img \s*id= "Main-image" \s*src= "(http://ecx\.images-mazon\.com/images/i/.+\. ( Jpg
------Solution--------------------
Gif
------Solution--------------------
Jpeg
------Solution--------------------
PNG)) "#U";
Preg_match ($p, $str, $result) && var_dump ($result);

Depending on the situation, you can also parse to the DOM:
$html = ";
$doc = new DOMDocument ();
$doc->loadhtml ($html);
echo $doc->getelementbyid (' Main-image ')->getattribute (' src ');


After 100,000 cycles of testing, the latter is 7 times times faster than the former, and the gap may be greater as the size of the test source string increases.
------Solution--------------------
$STR = ' img id= ' main-image "src=" http://ecx.images-mazon.com/images/I/41Auzc7RBPL._SY300_.jpg ";

------Solution--------------------
IMG id= "Main-image" src= "(. *)"
------Solution--------------------
IUs;
Preg_match ($p, $str, $result);
Print_r ($result);
If you only need to get to the URL of that picture name then you need to split
$ep =explode ('/', $result [0]);
Echo $ep [5];
  • 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.