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
Related
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
I want to extract the href of the anchor having only certain class with it like link-wrapper.
So, this means I will have the href of the link like:
click here
P.S. It should extract both the links if they are aligned in sequential manner like:
link-1link-2
I tried the solutions already present in the stack-overflow, but none suited my problem. Since some of them were in java-script and other languages. I tried looking for DOMDocument, but its bit difficult to exactly match the solution.
I tied some of the preg_match which didn't worked for me, like:
preg_match('/<a(?:(?!class\=")(?:.|\n))*class\="(?:(?!link\-wrapper)(?:.|\n))*link\-wrapper(?:(?!<\/a>)(?:.|\n))*<\/a>/i', $content, $output_array);
You can use DOMDocument and DOMXPath to get your results. First load the HTML into a DOMDocument and then use an XPath query to find all the anchors that have class including link-wrapper e.g.
$html = 'click herelink-3
link-1link-2';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a[contains(#class, "link-wrapper")]') as $a) {
$urls[] = $a->attributes->getNamedItem('href')->nodeValue;
}
foreach ($urls as $url) {
echo "$url\n";
}
Output:
blaa..blaa
blaa
blaa..again
Demo on 3v4l.org
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:
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