简体   繁体   中英

regex pattern to find this particular string in file_get_contents in PHP

I'm looking to get this value 'B00DS4KJR4' enclosed in tags from a url by using file_get_contents() function in PHP. However, I'm failing to write the correct regex to find the value from this html source code the page:

<span class="a-text-bold">ASIN:
                    </span>
                    <span>B00DS4KJR4</span>

Can you help me to write the correct regex to find that particular value on the page ?

You can use a regular expression like the following also presented on Regex101 . This looks for a <span> with any attributes, containing the string ASIN: in the innerHTML followed by another <span> and captures the contents of the second <span> .

$html ='<span class="a-text-bold">ASIN:
                </span>
                <span>B00DS4KJR4</span>';

if (preg_match('/<span\s[^><]*>\s*ASIN:\s*<\/span>\s*<span>\s*([^><]*)\s*<\/span>/i', $html, $m)) {
    $asin = $m[1];
    print $asin;
}
preg_match_all('/<span>(.*)<\/span>/',$the_html,$the_result_array);

第一个span与正则表达式不匹配,因为它具有类,其他的则写成<span>anything</span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM