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];