...

View Full Version : PHP cURL - preg_match_all



karabey
01-13-2012, 04:37 AM
Hi,

I am trying to get from the link which is XML some text to convert into link or drop down list. But somehow it will not work :(


$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://itunes.com/version?touchUpdate=true");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec ($curl);
curl_close ($curl);

//links
if(preg_match_all("~(.*)/iPhone/(.*)4\.0(.*)Restore", $result, $links))
{
foreach($links[0] as $link)
{
echo $link."<br />";
}
}


The XML Source File shows this


<key>iPhone2,1</key>
<dict>
<key>7A341</key>
<dict>
<key>OfferPartialRestoreAsUpdate</key>
<true/>
<key>Restore</key>
<dict>
<key>BuildVersion</key>
<string>7E18</string>
<key>DocumentationURL</key>
<string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7822.20100202.VfP8u/iPhoneDocumentation_3.1.3.ipd</string>
<key>FirmwareURL</key>
<string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7472.20100202.8tugj/iPhone2,1_3.1.3_7E18_Restore.ipsw</string>
<key>ProductVersion</key>
<string>3.1.3</string>
</dict>
</dict>
<key>7A400</key>
<dict>
<key>SameAs</key>
<string>7A341</string>
</dict>
<key>7C106c</key>
<dict>
<key>SameAs</key>
<string>7A341</string>
</dict>
<key>7C116a</key>
<dict>
<key>SameAs</key>
<string>7A341</string>
</dict>
<key>7C144</key>
<dict>
<key>SameAs</key>
<string>7A341</string>
</dict>
<key>7D11</key>
<dict>
<key>SameAs</key>
<string>7A341</string>
</dict>
<key>7E18</key>
<dict>
<key>Restore</key>
<dict>
<key>BuildVersion</key>
<string>7E18</string>
<key>DocumentationURL</key>
<string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7822.20100202.VfP8u/iPhoneDocumentation_3.1.3.ipd</string>
<key>FirmwareURL</key>
<string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7472.20100202.8tugj/iPhone2,1_3.1.3_7E18_Restore.ipsw</string>
<key>ProductVersion</key>
<string>3.1.3</string>
</dict>
</dict>
<key>Unknown</key>
<dict>
<key>Universal</key>
<dict>
<key>Restore</key>
<dict>
<key>BuildVersion</key>
<string>7E18</string>
<key>DocumentationURL</key>
<string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7822.20100202.VfP8u/iPhoneDocumentation_3.1.3.ipd</string>
<key>FirmwareURL</key>
<string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7472.20100202.8tugj/iPhone2,1_3.1.3_7E18_Restore.ipsw</string>
<key>ProductVersion</key>
<string>3.1.3</string>
<key>EpochVersion</key>
<string>2</string>
</dict>
</dict>
</dict>
</dict>


The Text i want to get is


<key>iPhone2,1</key>

and


<string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7472.20100202.8tugj/iPhone2,1_3.1.3_7E18_Restore.ipsw</string>


New some advice how i can do that.. Thanks

Spookster
01-13-2012, 05:31 AM
Since you captured an XML string why not just use an XML library to parse it. Take $result and load it into SimpleXML and then you can use the structure that the SimpleXML library creates to grab anything you need from the XML data.

http://us.php.net/manual/en/function.simplexml-load-string.php

And you can also use DOM in much the same way.
http://us.php.net/manual/en/domdocument.loadxml.php

karabey
01-13-2012, 03:14 PM
Thanks for your reply but i am newbie at PHP.. What i could do is without your tips.



$url = "http://itunes.com/version?touchUpdate=true";
$page_all = file_get_contents($url);

preg_match_all('~http://(.*)/iP(.*)Restore.ipsw~', $page_all, $div_array, PREG_PATTERN_ORDER);

echo "<pre>";
print_r($div_array[0]);
echo "</pre>";


This extract the needed files but it doesn't filter same Texts.

Otherwise i have a Code that converts Text to Link and does only one Link.. Rest is not shown.


<?php


$content = file_get_contents('http://itunes.com/version?touchUpdate=true');

$matches = "~(.*)/iP(.*)Restore~";


if(preg_match($matches, $content, $url)) {


echo preg_replace($matches, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $matches);

}


?>

Spookster
01-13-2012, 04:35 PM
What you are doing is way more complicated. Use my suggestion and use and XML library. Would only take a few lines of code to accomplish.

karabey
01-14-2012, 12:18 AM
As i have followed the Manuals you gave me, i could not find how to read the target.

Example


<?php

$xml = simplexml_load_file('http://itunes.com/version?touchUpdate=true');

?>


Sorry i am newbie and have to read so much.. It takes time :rolleyes:

Spookster
01-14-2012, 12:27 AM
You would use your existing cURL code to grab the feed because you told it to return a string containing the XML data.

This should give you the XML data in a SimpleXML structure. Then you can just grab the value of the node that you want using that structure syntax.


$result = curl_exec ($curl);
$xml = simplexml_load_string($result);
var_dump($xml);

karabey
01-14-2012, 12:59 AM
Ok most is done :D



<?php


$URL = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/com.apple.jingle.appserver.client.MZITunesClientCheck/version/?touchUpdate=true";


$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, 0);


$content = curl_exec($ch);

$result = curl_getinfo($ch);

$reg_exUrl = "~(.*)/iP(.*)Restore.ipsw~";

if(preg_match_all($reg_exUrl, $content, $result, PREG_PATTERN_ORDER));
{
foreach($result[0] as $link)
{
echo preg_replace($reg_exUrl, '<a href="'.$link.'">'.$link.'</a><br />', $link);
}
}
curl_close($ch);
?>


While the Source has same Text more than one it shows again.. Is there a way to show only one instead to repeat the same.

Result of Code

http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-5863.20090127.rt56K/iPod2,1_2.2.1_5H11a_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-5863.20090127.rt56K/iPod2,1_2.2.1_5H11a_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-5863.20090127.rt56K/iPod2,1_2.2.1_5H11a_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7481.20100202.4orot/iPhone1,1_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7481.20100202.4orot/iPhone1,1_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7481.20100202.4orot/iPhone1,1_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7481.20100202.4orot/iPhone1,1_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7468.20100202.pbnrt/iPhone1,2_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7468.20100202.pbnrt/iPhone1,2_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7468.20100202.pbnrt/iPhone1,2_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7472.20100202.8tugj/iPhone2,1_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7472.20100202.8tugj/iPhone2,1_3.1.3_7E18_Restore.ipsw
http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-7472.20100202.8tugj/iPhone2,1_3.1.3_7E18_Restore.ipsw

Links are show like this


<a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br /><a href=" <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw"> <string>http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iPhone/061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_Restore.ipsw</a><br />

Spookster
01-14-2012, 10:36 AM
I'm done helping you. I've done everything but completely write the code for you and you still keep going down the path of making it way over complicated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum