PHP get a div from page x [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I wanna extract some html from page Y
for example site is
<head>xxxx</head>
<body>....<div id="ineedthis_code"> </div> ...</body>
it is possible to do this file_get_contents ?!
i need only that div nothing else

Without using a special library (which is the best way in most cases), you can use the explode-function:
$content = file_get_contents($url);
$first_step = explode( '<div id="YOUR ID HERE">' , $content ); // So you will get two array elements
$second_step = explode("</div>" , $first_step[1] ); // "1" depends, if you have more elements with this id (theoretical)
echo $second_step[0]; // You will get the first element with the content within the DIV :)
Please note, it's only an example without error handling. It also works onlny on a special case; not if the html structure ist changing. Even simple spaces can break this code. So you should better use a parsing library ;-)

Related

is there a way to reestructure file_get_contents output? php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Im triyng to take some information from a json file,
the code in that page is like this
{"asignaciones":[{"fecha":"LUNES
29/07","horaEntrada":"18:30","horaSalida":"22:30","tienda":" BK Villa
Urquiza"}],"fechaConsulta":"29/07/2019 17:27","legajo":"28907"}
i want to take "29/7", "18:30", "22:30".
For now, i can print all the code in my page, but i want to take only those numbers, there is a way with:file_get_contents?
i'm trying to learn a little more php sorry if this question is oviously simple.
my code now:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
echo $content
?>
It looks like the content is a json... Start from this:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
$content = json_decode($content);
print_r($content);

PHP getting specific tags from file_get_contents [duplicate]

This question already has answers here:
How to get content from specific tags when using file_get_contents
(2 answers)
Closed 8 years ago.
$landings = file_get_contents('http://www.domain.co.uk/page.php');
In the above example URL I want to only select and copy the HTML from a specific element #sidebar-bgbtm
From there I would like to export the HTML as a JSON value.
Is there any way of doing that?
Thanks
If you are familiar with jQuery syntax, PHP Simple HTML DOM might be good place to start
http://simplehtmldom.sourceforge.net/
include('simple_html_dom.php');
$html = file_get_html('http://www.domain.co.uk/page.php');
$result = $html->find('#sidebar-bgbtm', 0)->outertext;
And then export to json:
echo json_encode( array( 'outertext' => $result) );

How to find css link from html page [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have php regex to find tag and extract css address from html page
'/<link.*?href\s*=\s*["\']([^"\']+)[^>]*>.*?\/>/i'
but it doesn't work good.can you help me to modify this code?
Perhaps
'/<link .*?(href=[\'|"](.*)?[\'|"]|\/?\>)/i'
Then you can acces the link with $2
Not that this is better than the other answer, however just in case you want to see it, I've altered your regex such that it should work as intended:
'/<link.*?href\s*=\s*["\']([^"\']+?)[\'"]/i'
Regex to find hrefs of all stylesheets can be a tricky task. You should consider using some PHP HTML parser to get this information.
You can read this article to get more information and then try this code.
// Retrieve all links and print their HREFs
foreach($html->find('link') as $e)
echo $e->href . '<br>';
// Retrieve all script tags and print their SRCs
foreach($html->find('script') as $e)
echo $e->src . '<br>';
PS: Remember, your script tag may not contain a src then it will print empty string.

Print out GData title in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have this link
http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1
That gives me the latest video from GudjoDaneel but I'd like to print our this title inside a PHP file
<title type='text'>The GD Project S3 | NEVER GIVE UP! | Division 1</title><content type='text'>
I'd appreciate it if someone could help me where to begin. And what I could look up.
I'd suggest looking up SimpleXML. It's easy to use once you get the hang of it, and you can get the title in just four line:
$url = 'http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1';
$source = file_get_contents($url);
$xml = new SimpleXMLElement($source);
$title = $xml->entry->title;
Do note, though, that $title is a PHP object in this case. If you echo it straight away, it'll be reinterpreted as a string, and everything will be alright. If you plan on doing anything else with it, you'll need to cast it as a string, like this:
$title = strval($title);

Tags still remaining after using simplexml_load_string [duplicate]

This question already has answers here:
Getting actual value from PHP SimpleXML node [duplicate]
(4 answers)
Closed 8 years ago.
I am using simplexml_load_string for XML packets. In my scenario, the XML string I want to convert is known as k.
My problem, however, is that when I use k, tags still remain that weren't parsed (<k>, <\k>).
For example, I use
$x->k, and I get back <k>DATA I WANT HERE<\EK>.
How do I get rid of these?
What the code does: It connects to a game and logs in.
Use InnerNode to get the value without the tags:
$x->k->InnerNode
You can also do a typecast:
(string)$x->k
I tried this and seem to be getting the string.
<?php
$str = "<msg t='sys'><body action='rndK' r='-1'><k>qH~e9Gmt</k></body></msg>";
$xml = simplexml_load_string( $str );
echo $xml->body->k; // gives 'qH~e9Gmt'
?>

Categories