This question already has answers here:
Grabbing the href attribute of an A element
(10 answers)
Closed 9 years ago.
I'm new to php and and i need help with something.
I need to divide the image and the content from this variable. It has a image and the description.
$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! #quote";
I know this is something simple but please help me. I need the image in a variable and the content with the link in another..
Thanks.
As #Barmar says, you should use a DOM parsing library.
You may find very useful this: http://simplehtmldom.sourceforge.net
Its use is similar to a jQuery parsing, and you only have to read doc to know how to use it (very good examples => http://simplehtmldom.sourceforge.net/manual.htm)
Example:
$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! #quote";
$html = str_get_html($content);
$image = $html->find('img');
$links = $html->find('a');
And you'll have what you want in $image and $links, :D
Related
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) );
This question already has answers here:
how to use dom php parser
(4 answers)
Closed 9 years ago.
<?php
$html = file_get_contents('http://xpool.xram.co/index.cgi');
echo $html;
?>
I want to get information in a tag on a remote web site using php. and only the tags.
I found this small string that is great for retrieving the entire site source. However, i want to get a small section only. How can I filter out all the other tags and get only the one tag I need?
I'd suggest using a PHP DOM parser. (http://simplehtmldom.sourceforge.net/manual.htm)
require_once ('simple_html_dom.php');
$html = file_get_contents('http://xpool.xram.co/index.cgi');
$p = $html->find('p'); // Find all p tags.
$specific_class = $html->find('.classname'); // Find elements with classname as class.
$element_id = $html->find('#element'); // Find element with the id element
Read the docs, there are tons of other options available.
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.
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);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Regex to change format of all img src attributes
Hi,
I want to replace the image path in my content db field.
I have the following
preg_replace("/src='(?:[^'\/]*\/)*([^']+)'/g","src='newPath/$2'",$content);
which is working fine for
src="/path/path/image.jpg"
BUT fails ON
src="http://www.mydomain.com/path/path/image.jpg"
Any help to bypass this problem?
Don't use regular expressions for this. Use a HTML parser like Simple HTML DOM.
$html = file_get_html('http://www.example.com/sourcepage.html');
foreach($html->find('img') as $element)
{
$new_src = "Do stuff with new src here";
$element->src = $new_src;
}
echo $html; // Output new code