Log in

View Full Version : I need a preg_match pattern



mda_omega
10-31-2010, 06:44 AM
I have a site that have these lines involved:


...
<div class="post_icon">
<div class="post_icon_overlay"> </div>
<img src="http://localhost/wp/65a39792ad0fc80.jpg">
</div>
...


I have made a function that is trying to grab the img address. But I cannot make a good pattern for it.


function GetImg($pageurl){
$contents = @file_get_contents($pageurl);
if (!$contents) return FALSE;
preg_match(' ??? ', $contents, $matches);
return $matches[1];
}

can any one help?

bluewalrus
10-31-2010, 07:10 AM
If that is the exact pattern and that is a tab i think this should do it..


'/.*?class="post_icon".*?\t<img src="(.*?)">/s'

mda_omega
10-31-2010, 08:26 AM
If that is the exact pattern and that is a tab i think this should do it..


'/.*?class="post_icon".*?\t<img src="(.*?)">/s'

Works. :D thank you.

mda_omega
10-31-2010, 10:36 AM
what about all images that has the same prefix url ?


http://localhost/2450/

I cannot change the function, may you please change it for me? these are examples


http://localhost/2450/ss_3ba0cf8efe4c69a96fe6.600x338.jpg?t=1288373899
http://localhost/2450/sfeegfba0cf84c69a9erf6fe6.600x338.jpg?t=1288373899
http://localhost/2450/ss_3gfef84ce6efgrg4et46.600x338.jpg?t=1288373899
http://localhost/2450/ssk_3bafwef0cfuc69a96fe6.600x338.jpg?t=1288373899

james438
10-31-2010, 03:14 PM
can we see the image addresses in context? We need to see what separates one image address from another. Worded another way: what does the code look like where at least one of the image addresses is located.

bluewalrus, why can't the first pattern be simplified just a wee bit to <img src="(.*?)">?

bluewalrus
10-31-2010, 04:07 PM
The first pattern was to pull the images contained in post_icon.

I think per the new requirement this should work. If not we'd need to see the actual code.


'/<img src="(http:\/\/localhost\/2450\/.*?)">/'

james438
10-31-2010, 06:39 PM
yes, but that is not what he was asking for. or at least I am not seeing that in his initial post.
Either way, I would like to see the actual code mda_omega.

mda_omega
11-01-2010, 12:51 PM
you both were right :D I needed something between what you said. all the images that has the same address. I think I should use preg_match_all instead. Am I right?



$contents = @file_get_contents($pageurl);
if (!$contents) return FALSE;
preg_match_all('/<img src="(http:\/\/localhost\/2450\/.*?)">/', $contents, $matches);


So. the other problem is. How can I reach them one by one?

is it good?


echo $matches[0];
echo $matches[1];
echo $matches[2];
...

I dont need to get errors, so, how can I figure out how many they are? Do I need a loop to use them one by one??

bluewalrus
11-01-2010, 01:04 PM
You can use the foreach function

http://php.net/manual/en/control-structures.foreach.php

so it'd be


foreach ($matches as $value) {
echo $value;
}

assuming that array has all the values you want.

mda_omega
11-01-2010, 01:23 PM
thank you thank you thank you :xxx

That was simple and effective. :D