Jump to content

Reading xml files...and a stupid substr problem


DamienRoche

Recommended Posts

I have no idea how I should be doing this and I am in desperate need of help.

 

I have an xml file. At the beginning is this:

 

<ResultSet xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/SiteExplorerService/V1/InlinkDataResponse.xsd" totalResultsAvailable="547535" firstResultPosition="1" totalResultsReturned="50">

 

How do I simply put 'totalResultsAvailable' (547535) into a variable?

 

I have tried using substr and have successfully singled out the number but it is forever trapped between two double quotes.

 

Any help on this is greatly appreciated.

Probably a far easier way, but try this out.

 

<?php

$str = '<ResultSet xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/SiteExplorerService/V1/InlinkDataResponse.xsd" totalResultsAvailable="547535" firstResultPosition="1" totalResultsReturned="50">';

preg_match('#totalResultsAvailable="(.*?)"#is', $str, $match);

$totalResultsAvailable = $match[1];

?>

Hey, thanks for the reply!- sure enough, that code works, but for some reason isn't working for this:

 

<?xml version='1.0' encoding='UTF-8'?> <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:srch" xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/SiteExplorerService/V1/InlinkDataResponse.xsd" totalResultsAvailable="40626" firstResultPosition="1" totalResultsReturned="50">  <Result>  <Title>Time Inc. Portal - Pathfinder: Your Guide to the Websites of Time Inc.</Title>  <Url>http://www.pathfinder.com/</Url>  <ClickUrl>http://www.pathfinder.com/</ClickUrl>  </Result>  <Result>  <Title>The Big Picture</Title>  <Url>http://bigpicture.typepad.com/</Url>  <ClickUrl>http://bigpicture.typepad.com/</ClickUrl>  </Result>  <Result>  <Title>Essential Links</Title>  <Url>http://www.el.com/</Url>  <ClickUrl>http://www.el.com/</ClickUrl>  </Result>  <Result>  <Title>SaveOnRefinance.com</Title>  <Url>http://www.saveonrefinance.com/</Url>  <ClickUrl>http://www.saveonrefinance.com/</ClickUrl>  </Result>  <Result>  <Title>Managerial Accounting Fundamentals Website</Title>  <Url>http://members.tripod.com/Acct202/</Url>  <ClickUrl>http://members.tripod.com/Acct202/</ClickUrl>  </Result>  <Result>  <Title>The Big Picture</Title>  <Url>http://bigpicture.typepad.com/comments/</Url>  <ClickUrl>http://bigpicture.typepad.com/comments/</ClickUrl>  </Result>  <Result>  <Title>Investor Home</Title>  <Url>http://www.investorhome.com/</Url>  <ClickUrl>http://www.investorhome.com/</ClickUrl>  </Result>  <Result>  <Title>Backlash</Title>  <Url>http://www.backlash.com/</Url>  <ClickUrl>http://www.backlash.com/</ClickUrl>  </Result>  <Result>  <Title>Time Inc. Portal</Title>  <Url>http://cgi.pathfinder.com/</Url>  <ClickUrl>http://cgi.pathfinder.com/</ClickUrl>  </Result>  <Result>  <Title>The Big Picture | The Costanza Energy Policy: 25 Ways to Drive Oil to $150</Title>  <Url>http://bigpicture.typepad.com/comments/2008/05/how-to-drive-oi.html</Url>  <ClickUrl>http://bigpicture.typepad.com/comments/2008/05/how-to-drive-oi.html</ClickUrl>  </Result>  <Result>  <Title>The Big Picture | DRM Crippled CD: A bizarre tale in 4 parts</Title>  <Url>http://bigpicture.typepad.com/comments/2005/10/drm_crippled_cd.html</Url>  <ClickUrl>http://bigpicture.typepad.com/comments/2005/10/drm_crippled_cd.html</ClickUrl>  </Result>  <Result>  <Title>Global Satellite Chart--Geo-Orbit Quick-Look--Western Hemisphere--C/Ku ...</Title>  <Url>http://www.geo-orbit.org/</Url>  <ClickUrl>http://www.geo-orbit.org/</ClickUrl>  </Result>  <Result>  <Title>Torrid Technologies, Inc.</Title>  <Url>http://www.torrid-tech.com/</Url>  <ClickUrl>http://www.torrid-tech.com/</ClickUrl>  </Result>  <Result>  <Title>Welcome to HouseList Free Real Estate Listings</Title>  <Url>http://www.houselist.com/</Url>  <ClickUrl>http://www.houselist.com/</ClickUrl>  </Result>  <Result>  <Title>Seven Mile</Title>  <Url>http://frankschilling.typepad.com/</Url>  <ClickUrl>http://frankschilling.typepad.com/</ClickUrl>  </Result>  <Result>  <Title>Making money mistakes? Take our quiz! - Today Technology &amp; Money ...</Title>  <Url>http://www.msnbc.msn.com/id/6779198/</Url>  <ClickUrl>http://www.msnbc.msn.com/id/6779198/</ClickUrl>  </Result>  <Result>  <Title>Jay's Pop Music Pages - Artists, Radio &amp; TV, Music Stores, Record ...</Title>  <Url>http://www.stormloader.com/popmusic/home.htm</Url>  <ClickUrl>http://www.stormloader.com/popmusic/home.htm</ClickUrl>  </Result>  <Result>  <Title>The Big Picture | The NonGuidebook Version of What to Do (and Not Do ...</Title>  <Url>http://bigpicture.typepad.com/comments/2008/05/guide-for-new-y.html</Url>  <ClickUrl>http://bigpicture.typepad.com/comments/2008/05/guide-for-new-y.html</ClickUrl>  </Result>  <Result>  <Title>Own, Be Owned Or Remain Invisible.</Title>  <Url>http://www.irational.org/heath/_readme.html</Url>  <ClickUrl>http://www.irational.org/heath/_readme.html</ClickUrl>  </Result>  <Result>  <Title>MONEY Magazine: Best places to live 2007: Top 100</Title>  <Url>http://money.cnn.com/magazines/moneymag/bplive/2007/top100/</Url>  <ClickUrl>http://money.cnn.com/magazines/moneymag/bplive/2007/top100/</ClickUrl>  </Result>  <Result>  <Title>Magazines - Grizzly Web Links</Title>  <Url>http://grizzlyweb.com/links/magazine.asp</Url>  <ClickUrl>http://grizzlyweb.com/links/magazine.asp</ClickUrl>  </Result>  <Result>  <Title>The Big Picture | Countries GDP as US States</Title>  <Url>http://bigpicture.typepad.com/comments/2007/01/countries_gdp_a.html</Url>  <ClickUrl>http://bigpicture.typepad.com/comments/2007/01/countries_gdp_a.html</ClickUrl>  </Result>  <Result>  <Title>4 Insurance Agents</Title>  <Url>http://www.4insuranceagents.com/</Url>  <ClickUrl>http://www.4insuranceagents.com/</ClickUrl>  </Result>  <Result>  <Title>Christian Money</Title>  <Url>http://www.christianmoney.com/</Url>  <ClickUrl>http://www.christianmoney.com/</ClickUrl>  </Result>  <Result>  <Title>Digital Librarian: Reference</Title>  <Url>http://www.digital-librarian.com/reference.html</Url>  <ClickUrl>http://www.digital-librarian.com/reference.html</ClickUrl>  </Result>  <Result>  <Title>Ten things to save money in 2004 - Today Technology &amp; Money - MSNBC.com</Title>  <Url>http://www.msnbc.msn.com/id/3841990/</Url>  <ClickUrl>http://www.msnbc.msn.com/id/3841990/</ClickUrl>  </Result>  <Result>  <Title>Fed cuts help...sort of - Aug. 21, 2001</Title>  <Url>http://money.cnn.com/2001/08/21/home_auto/q_rates/</Url>  <ClickUrl>http://money.cnn.com/2001/08/21/home_auto/q_rates/</ClickUrl>  </Result>  <Result>  <Title>TIMEasia.com | TIME 100: Sun Yat-sen | 8/23/99-8/30/99</Title>  <Url>http://www.time.com/time/asia/asia/magazine/1999/990823/sun_yat_sen1.html</Url>  <ClickUrl>http://www.time.com/time/asia/asia/magazine/1999/990823/sun_yat_sen1.html</ClickUrl>  </Result>  <Result>  <Title>Money Magazine Subscription Offer</Title>  <Url>http://subs.timeinc.net/CampaignHandler/MOnb?source_id=16</Url>  <ClickUrl>http://subs.timeinc.net/CampaignHandler/MOnb?source_id=16</ClickUrl>  </Result>  <Result>  <Title>Investor's Guide - Middletown Thrall Library</Title>  <Url>http://www.thrall.org/invest</Url>  <ClickUrl>http://www.thrall.org/invest</ClickUrl>  </Result>  <Result>  <Title>Turner Broadcasting System - Wikipedia, the free encyclopedia</Title> <Url>http://en.wikipedia.org/wiki/Turner_Broadcasting_System</Url> <ClickUrl>http://en.wikipedia.org/wiki/Turner_Broadcasting_System</ClickUrl> </Result> <Result> <Title>Alfalfa Electric Cooperative - Cherokee, OK</Title> <Url>http://www.alfalfaelectric.coop/</Url> <ClickUrl>http://www.alfalfaelectric.coop/</ClickUrl> </Result> <Result> <Title>PrescottYellowPages.com</Title> <Url>http://prescottyellowpages.com/</Url> <ClickUrl>http://prescottyellowpages.com/</ClickUrl> </Result> <Result> <Title>The Big Picture | Lender-Abandoned, Non-REO Foreclosures</Title> <Url>http://bigpicture.typepad.com/comments/2008/04/the-advantages.html</Url> <ClickUrl>http://bigpicture.typepad.com/comments/2008/04/the-advantages.html</ClickUrl> </Result> <Result> <Title>Pay off holiday debt in the new year - Holiday Guide - MSNBC.com</Title> <Url>http://www.msnbc.msn.com/id/6531029/</Url> <ClickUrl>http://www.msnbc.msn.com/id/6531029/</ClickUrl> </Result> <Result> <Title>Insurance - Auto Insurance, Home, Health, etc. from CNNMoney.com</Title> <Url>http://money.cnn.com/pf/insurance/</Url> <ClickUrl>http://money.cnn.com/pf/insurance/</ClickUrl> </Result> <Result> <Title>TIMEasia.com | TIME 100: Emperor Hirohito | 8/23/99-8/30/99</Title> <Url>http://www.time.com/time/asia/asia/magazine/1999/990823/hirohito1.html</Url> <ClickUrl>http://www.time.com/time/asia/asia/magazine/1999/990823/hirohito1.html</ClickUrl> </Result> <Result> <Title>The Big Picture | Why is Movie Theatre Revenue Attendance Declining?</Title> <Url>http://bigpicture.typepad.com/comments/2005/07/declining_movie.html</Url> <ClickUrl>http://bigpicture.typepad.com/comments/2005/07/declining_movie.html</ClickUrl> </Result> <Result> <Title>Home Page</Title> <Url>http://www.personal.psu.edu/faculty/j/x/jxz8/Student_Webquests/Neu_Sel_Yar/Questts/Ourquest</Url> <ClickUrl>http://www.personal.psu.edu/faculty/j/x/jxz8/Student_Webquests/Neu_Sel_Yar/Questts/Ourquest</ClickUrl> </Result> <Result> <Title>Papal Succession Primer</Title> <Url>http://www.time.com/time/daily/special/papacy/how.html</Url> <ClickUrl>http://www.time.com/time/daily/special/papacy/how.html</ClickUrl> </Result> <Result> <Title>TIMEasia.com | TIME 100: Pol Pot | 8/23/99-8/30/99</Title> <Url>http://www.time.com/time/asia/asia/magazine/1999/990823/pol_pot1.html</Url> <ClickUrl>http://www.time.com/time/asia/asia/magazine/1999/990823/pol_pot1.html</ClickUrl> </Result> <Result> <Title>It's spring: Time for a little exercise - Mar. 15, 2004</Title> <Url>http://money.cnn.com/2004/03/12/commentary/everyday/sahadi/</Url> <ClickUrl>http://money.cnn.com/2004/03/12/commentary/everyday/sahadi/</ClickUrl> </Result> <Result> <Title>Taking advantage of lower interest rates - Jun. 27, 2001</Title> <Url>http://money.cnn.com/2001/06/27/home_auto/fed_consumer/</Url> <ClickUrl>http://money.cnn.com/2001/06/27/home_auto/fed_consumer/</ClickUrl> </Result> <Result> <Title>How to get rich in America - Smoothie operators (1) - Money Magazine</Title> <Url>http://money.cnn.com/galleries/2007/moneymag/0706/gallery.success_stories.moneymag/index.html</Url> <ClickUrl>http://money.cnn.com/galleries/2007/moneymag/0706/gallery.success_stories.moneymag/index.html</ClickUrl> </Result> <Result> <Title>The Big Picture | The Return of M3</Title> <Url>http://bigpicture.typepad.com/comments/2006/11/the_return_of_m.html</Url> <ClickUrl>http://bigpicture.typepad.com/comments/2006/11/the_return_of_m.html</ClickUrl> </Result> <Result> <Title>TIMEasia.com</Title> <Url>http://www.time.com/time/asia/asia/magazine/1999/990719/souls1.html</Url> <ClickUrl>http://www.time.com/time/asia/asia/magazine/1999/990719/souls1.html</ClickUrl> </Result> <Result> <Title>The Big Picture | How SubPrime Really Works</Title> <Url>http://bigpicture.typepad.com/comments/2008/02/how-subprime-re.html</Url> <ClickUrl>http://bigpicture.typepad.com/comments/2008/02/how-subprime-re.html</ClickUrl> </Result> <Result> <Title>Consumer World: Money, Credit and Investing</Title> <Url>http://www.consumerworld.org/pages/money.htm</Url> <ClickUrl>http://www.consumerworld.org/pages/money.htm</ClickUrl> </Result> <Result> <Title>welcome-to-china.com, Best web sites Resources, Top 100 Sites, Top web ...</Title> <Url>http://www.welcome-to-china.com/</Url> <ClickUrl>http://www.welcome-to-china.com/</ClickUrl> </Result> <Result> <Title>The Big Picture | We've gone Map Crazy!</Title> <Url>http://bigpicture.typepad.com/comments/2004/11/weve_gone_map_c.html</Url> <ClickUrl>http://bigpicture.typepad.com/comments/2004/11/weve_gone_map_c.html</ClickUrl> </Result> </ResultSet> <!-- fe03.sitex.search.sk1.yahoo.com uncompressed/chunked Thu Oct 9 01:31:06 PDT 2008 --> <!-- ws04.search.scd.yahoo.com uncompressed Thu Oct 9 01:31:06 PDT 2008 -->

 

Do you have any idea why? it seems so simple on the surface but it's absolutely rediculous how I can't do this with a simple preg_match or substr.

 

Any other advice is welcomed.

 

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.