This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I'm using preg_match_all to import images using the
Currently works:
//get image url from url
preg_match_all('/<img[^>]+>/i',$file->body, $images);
I tried this:
//get image url from url
preg_match_all('/<img id="charity"[^>]+>/i',$file->body, $images);
Once altered I get a 500 server error so I assume my syntax is wrong. How do I correctly alter this to work correctly?
That regexp seems to be OK. Your error must be anywere else.
You can test it with something like:
<?php
$test='
<img src="http://example.com/image.jpg"/>
foo bar
<img id="charity" src="local/image.jpg"/>
';
preg_match_all('/<img[^>]+>/i',$test, $images);
print_r($images);
preg_match_all('/<img id="charity"[^>]+>/i',$test, $images);
print_r($images);
Output:
Array
(
[0] => Array
(
[0] => <img src="http://example.com/image.jpg"/>
[1] => <img id="charity" src="local/image.jpg"/>
)
)
Array
(
[0] => Array
(
[0] => <img id="charity" src="local/image.jpg"/>
)
)
Tested with PHP 5.2.13.
Related
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 3 years ago.
with the URL
domain.com/index.php?option=component&ctrl=product&task=show&cid=5076
$parse = parse_url( $full_url, PHP_URL_QUERY);
echo $parse['ctrl']; // I get nothing
How can I get my values from keys (option, ctrl, task, and cid)?
You can use parse_str along with parse_url,
$full_url = "domain.com/index.php?option=component&ctrl=product&task=show&cid=5076";
parse_str(parse_url($full_url,PHP_URL_QUERY), $arr); // parse query string
print_r($arr);
Demo
Output
Array
(
[option] => component
[ctrl] => product
[task] => show
[cid] => 5076
)
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
i'd like to get more than one result using the fuction mentioned in the title, so far i'm just able to get just an only value using preg_match. I'd like to get an array of all the results and then record them in my db.
That's my current code:
$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match_all('|<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="|' , $cast , $castimg );
print_r($castimg);
When i print the results i'm just giving: Array ( [0] => Array ( [0] =>
I made sure cast contains what i want. I've tried already a lot of possibilities and i got nothing :(
Try this one. I hope you grabbing all image URL's
$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match_all('#<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="#' , $cast , $castimg );
print_r($castimg[1]);
Output
Array ( [0] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/d9NkfCwczP0TjgrjpF94jF67SK8.jpg [1] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/jdRmHrG0TWXGhs4tO6TJNSoL25T.jpg [2] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/gzjmHOSM1vnwnXpU737tdu9YjOu.jpg [3] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/zYiS1KJKEoLstzFA3DV5gwszzSC.jpg [4] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/vDuvdRoliXbjQrptk7GVs4Qj07S.jpg ......)
If you use
var_dump($castimg);
instead
print_r($castimg);
you will see much more clear.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
Trying to capture all text between tags.
Code:
$test = '<test>foo<tests> asdlkfjklas lkflsdakj <test>sdfsd<tests> asdlkaskl <test>235234<tests>';
$match = '/<test>(.*)<tests>/';
preg_match_all($match, $test, $nextLink);
Result of print_r:
Array ( [0] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) [1] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) )
your regex syntax is greedy. use folowing:
$match = '/<test>(.*?)<tests>/';
This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 8 years ago.
why is this wrong?
[enclosure] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345&
[length] =>
[type] => image/jpeg
)
)
I want to get the url to get the image file
I wrote print_r($eachItem->enclosure['#attributes']->url) it doesn't work. Why?
That is not the correct way of getting the attribute value. Use ->attributes() method:
echo (string) $eachItem->enclosure->attributes()['url'];
// as of PHP 5.4 (dereferencing)
Or
// PHP 5.3 below
$eachItem_attribute = $eachItem->enclosure->attributes();
echo (string) $eachItem_attribute['url'];
Right & Quick Format
$eachItem->enclosure->attributes()->{'url'};
This question already has answers here:
How to extract img src, title and alt from html using php? [duplicate]
(10 answers)
Closed 8 years ago.
I'd like to extract the img src and using preg_match_all I have this:
$tag = '<img src="path/to/image.png" />';
preg_match_all('/(width|height|src)=("[^"]*")/i',$tag, $img[$tag]);
which returns:
Array
(
[<img src="path/to/image.png" />] => Array
(
[0] => Array
(
[0] => src="path/to/image.png"
)
[1] => Array
(
[0] => src
)
[2] => Array
(
[0] => "path/to/image.png"
)
)
)
How can I write the regex to return a similar result regardless of double or single quotes used in tag? I can write:
$tag = "<img src='path/to/image.png' />";
preg_match_all('/(width|height|src)=(\'[^\']*\')/i',$tag, $img[$tag]);
Which works, but I'm not familiar enough with regex to write one expression to handle either. I did try:
preg_match_all('/(width|height|src)=((\'[^\']*\')|("[^"]*"))/i',$tag, $img[$tag]);
But this seems to return extra matches in the array which I don't want.
You can use this:
(width|height|src)=("[^"]*"|'[^']*')
I've basically used an alternation to either match "fds" or 'fds'.