M
M
midarovrk2018-04-11 20:36:05
PHP
midarovrk, 2018-04-11 20:36:05

How to display and crop a piece of code from a site?

Hello.
Help, how to cut off all unnecessary from the existing code on the site and get the necessary information?
The website has the following code:

<div class="ForRead">
<div class="navigation">
<a href="/online-reading/comicsonline/batman2016vol3/batman2016vol3042" class="leftC"></a>
<select class="C" onchange="window.location='/online-reading/comicsonline/batman2016vol3/batman2016vol3043/'+this.value">
<option value="1" selected="">-1-</option><option value="2" >-2-</option><option value="3" >-3-</option><option value="4" >-4-</option><option value="5" >-5-</option><option value="6" >-6-</option><option value="7" >-7-</option><option value="8" >-8-</option><option value="9" >-9-</option><option value="10" >-10-</option><option value="11" >-11-</option><option value="12" >-12-</option><option value="13" >-13-</option><option value="14" >-14-</option><option value="15" >-15-</option><option value="16" >-16-</option><option value="17" >-17-</option><option value="18" >-18-</option><option value="19" >-19-</option><option value="20" >-20-</option>
</select>
<a href="/online-reading/comicsonline/batman2016vol3/batman2016vol3043/2" class="rightC"></a>
</div> 
<a href="http://comicsonline.ru/1/batman2016vol3/043/1.png" rel="shadowbox"><b>Увеличить</b></a>	    		
<a href="/online-reading/comicsonline/batman2016vol3/batman2016vol3043/2"><img src="http://comicsonline.ru/1/batman2016vol3/043/1.png?st=&e="
        		alt="Комиксы Онлайн - Бэтмен том 3 - # 43 - Страница №1 - Batman vol 3 - # 43"
        		title="Комиксы Онлайн - Бэтмен том 3 - # 43 - Страница №1 - Batman vol 3 - # 43" ></a>
</div>

From this code, you only need to display the url from img, this one
http://comicsonline.ru/1/batman2016vol3/043/1.png?st=&e=

And the number from the very last option tag is the number 20
to make it look like this:
http://comicsonline.ru/1/batman2016vol3/043/1.png?st=&e=
20

I was only able to render the entire select from within the block div, like so:
<?
$text = file_get_contents("http://site.com/index.php");
$data = array();

preg_match("/<div class=\"navigation\">(.*)<\/div>/Uis", $text, $out);

print "$out[1]";
?>

But getting only the last digit does not work, just like the url from img.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yan-s, 2018-04-11
@Yan-s

Do not write regular expressions for HTML parsing, there are more convenient solutions for this, for example
https://github.com/Imangazaliev/DiDOM
https://github.com/paquettg/php-html-parser

S
Stalker_RED, 2018-04-11
@Stalker_RED

You can use Zend\Dom\Query

use Zend\Dom\Query;
$dom = new Query($html);

$src = $dom->execute('img')->current()->getAttribute('src');
$opt = $dom->execute('select option[last()]')->current()->getAttribute('value'); 

echo $src . PHP_EOL . $opt;

From a more complex one, you can take Guzzle , where the http client is immediately included.

A
Artem, 2018-04-11
@proudmore

"Even Jon Skeet can't parse html with regular expressions" :)))
L5KvMJ3ExMg.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question