Retrieve data contained a certain span class

2020-04-21 08:06发布

using file_get_contents, I open an Internet URL and get the contents of this webpage.

Inside the HTML there are many identical span class tags:

<span class="always-the-same-class">always dynamic text</span>

Now, I want to get an array containing all the "dynamic text" contained in any of this tags. It is not necessary to eliminate duplicated entries (I need them).

Is this possible? How could I do?

标签: php
3条回答
祖国的老花朵
2楼-- · 2020-04-21 08:26

If I understood correctly, this has to be PHP as it is on the server, not in the browser. So I'd do something like

$html=file_get_contents(HTML_URL);
$a=preg_match_all("/\<span class\=\"always-the-same-class\"\>(.*?)\<\/span\>/",$html,$b);
echo $a;
print_r($b[1]);

$a has hit count, $b[1] the hits

Tested this against

<html>
.. blah ..
<body>

.. blah ..

<span class="always-the-same-class">always dynamic text A</span>
<span class="always-the-same-class">always dynamic text B</span>
<span class="always-the-same-class">always dynamic text C</span>

.. blah ..

</body>
</html>

and output was

3
Array
(
    [0] => always dynamic text A
    [1] => always dynamic text B
    [2] => always dynamic text C
)
查看更多
▲ chillily
3楼-- · 2020-04-21 08:29

You can parse this content using the DOMDocument class that is provided in PHP. Once you load the content into the dom document you can then filter out the span tags by using $content->getElementsByTagName('span'); Once you have done this then you can filter the results by the tags attributes and get the content.

查看更多
甜甜的少女心
4楼-- · 2020-04-21 08:39

jquery:

var spanText = $('.always-the-same-class').text();
查看更多
登录 后发表回答