{"id":3248,"date":"2008-06-19T18:34:33","date_gmt":"2008-06-20T01:34:33","guid":{"rendered":"http:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/?p=3248"},"modified":"2008-06-19T18:34:33","modified_gmt":"2008-06-20T01:34:33","slug":"speaking-in-tongues-parsing-structured-data-on-the-fly","status":"publish","type":"post","link":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/3248\/speaking-in-tongues-parsing-structured-data-on-the-fly\/","title":{"rendered":"Speaking in tongues: Parsing structured data on the fly"},"content":{"rendered":"<p>This is not ProjectBloodhound material, at least not first semester stuff. But if you find yourself running into highly structured data &#8212; such as the reports from a spreadsheet or a database application &#8212; you have the ability to easily manipulate that data in PHP.<\/p>\n<p>This is a simple example, but you don&#8217;t have to limit yourself to doing simple things. Imagine a data structure like this:<\/p>\n<p>Name[tab]Phone Number<br \/>\nCathleen Collins[tab]602-369-9275<br \/>\nGreg Swann[tab]602-740-7531<\/p>\n<p>In the file the code shown here as &#8220;[tab]&#8221; would be an actual tab character, and this kind of data goes by the arcane name of: A tab-delimited file.<\/p>\n<p>Most programming languages were written by exacting people with abstract and elegant reasons for everything they did. PHP was written by overbooked programmers who needed to pound out new web pages as quickly as possible.<\/p>\n<p>In consequence, PHP is optimized for dealing with highly structured data. Here is a short program that will take a tab-delimited phone number file as input and output reformatted phone numbers into the HTML stream. In other words, this code could produce a dynamically-updated phone list in what what might otherwise be a static web page:<\/p>\n<pre>&lt;?PHP\r\nauto_detect_line_endings;\r\n\r\n$fi = fopen(\"PhoneNums.txt\",\"r\");\r\n$line = fgets ($fi, 4096); \/\/ throw away fieldDef line\r\n\r\necho (\"&lt;b&gt;Phone Numbers&lt;\/b&gt;&lt;br&gt;\");\r\n\r\nwhile (!feof($fi))\r\n    {\r\n    $line = fgets ($fi, 4096);\r\n\r\n    list ($Name, $Phone_Number) = explode (\"\\t\", $line);\r\n\r\n    if ($Name)\r\n        {\r\n        echo (\"$Phone_Number &lt;i&gt;($Name)&lt;\/i&gt;&lt;br&gt;\");\r\n        }\r\n    }\r\n\r\nfclose ($fi);\r\n?&gt;<\/pre>\n<p>There is one line that makes all the difference for this kind of work:<\/p>\n<pre>    list ($Name, $Phone_Number) = explode (\"\\t\", $line);<\/pre>\n<p>The stuff between the parenthesis are our known field names, and we&#8217;re using them as variable names for clarity&#8217;s sake. The explode function will create an array of separate fields from the text stored in the $line variable, splitting the fields on the tab character. The list function then inherits the array just created by explode and assigns each field to the appropriate field name variables. We only have two fields <a href=\"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/3248\/speaking-in-tongues-parsing-structured-data-on-the-fly\/#more-3248\" class=\"more-link\">Read more<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is not ProjectBloodhound material, at least not first semester stuff. But if you find yourself running into highly structured data &#8212; such as the reports from a spreadsheet or a database application &#8212; you have the ability to easily manipulate that data in PHP. This is a simple example, but you don&#8217;t have to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,5,19],"tags":[],"class_list":{"0":"post-3248","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-marketing","7":"category-real-estate","8":"category-technology","10":"no-featured-image"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10139,"url":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/10139\/real-estate-sales-transactions-in-phoenix\/","url_meta":{"origin":3248,"position":0},"title":"Real Estate Sales Transactions In Phoenix","author":"Ryan Hartman","date":"October 20, 2009","format":false,"excerpt":"I don't know if the local board in Phoenix allows agents to display recent sales transactions via idx feed, but it seems to me from talking to others around the land that most boards don't. Doesn't it kinda sting that Trulia can display this info, but Realtor folk can't? Or\u2026","rel":"","context":"In &quot;Supplanting the NAR&quot;","block_context":{"text":"Supplanting the NAR","link":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/category\/supplanting-the-nar\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":582,"url":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/582\/tab-surfing-cataloging-my-collection-of-good-posts\/","url_meta":{"origin":3248,"position":1},"title":"Tab-surfing: Cataloging my collection of good posts&#8230;","author":"Greg Swann","date":"October 28, 2006","format":false,"excerpt":"It really is a matter of browser tabs. I live in Safari for the Mac, and if I see something in Vienna, my RSS feed reader, that I want to explore, I'll open the window, then slide the tab over to another open window that is already full of tabs.\u2026","rel":"","context":"In &quot;Blogging&quot;","block_context":{"text":"Blogging","link":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/category\/blogging\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1263,"url":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/1263\/random-observations-on-the-new-zillowcom-feature-set\/","url_meta":{"origin":3248,"position":2},"title":"Random observations on the new Zillow.com feature set","author":"Greg Swann","date":"April 4, 2007","format":false,"excerpt":"Your profile can include an embedded YouTube video. Your profile photo is going to be reduced to 66 pixels in width, so if you scale it to that size before uploading, you'll get marginally better quality. (Or is it 100 pixels tall?) In general, photo scaling seems pretty fuzzy to\u2026","rel":"","context":"In &quot;Marketing&quot;","block_context":{"text":"Marketing","link":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/category\/marketing\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":13151,"url":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/13151\/have-a-great-labor-day-weekend\/","url_meta":{"origin":3248,"position":3},"title":"Have a great Labor Day weekend!","author":"Tom Johnson","date":"September 3, 2010","format":false,"excerpt":"Let's stay away from the #RTB(tab) crowd as industry lifeguard comparison, shall we? Have a safe and relaxing weekend. h\/t Phil Martin","rel":"","context":"In &quot;Casual Friday&quot;","block_context":{"text":"Casual Friday","link":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/category\/casual-friday\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2465,"url":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/2465\/speaking-in-tongues-for-morgan-brown-a-quick-and-dirty-contributors-blogroll\/","url_meta":{"origin":3248,"position":4},"title":"Speaking in tongues for Morgan Brown: A quick and dirty contributors&#8217; blogroll","author":"Greg Swann","date":"January 3, 2008","format":false,"excerpt":"I know I promised to do nothing but \"includes,\" and we'll come back to those soon, but here is a real PHP routine, doing an actual real world job. What does it do? For a multi-author weblog like BloodhoundBlog, it produces a blogroll of the contributors' weblogs or web sites.\u2026","rel":"","context":"In &quot;Blogging&quot;","block_context":{"text":"Blogging","link":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/category\/blogging\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11435,"url":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/11435\/mark-steyn-when-responsibility-doesn%e2%80%99t-pay\/","url_meta":{"origin":3248,"position":5},"title":"Mark Steyn: &#8220;When Responsibility Doesn\u2019t Pay&#8221;","author":"Greg Swann","date":"February 28, 2010","format":false,"excerpt":"National Review Online:Think of Greece as California: Every year an irresponsible and corrupt bureaucracy awards itself higher pay and better benefits paid for by an ever-shrinking wealth-generating class. And think of Germany as one of the less profligate, still-just-about-functioning corners of America such as my own state of New Hampshire:\u2026","rel":"","context":"In &quot;Big Mother&quot;","block_context":{"text":"Big Mother","link":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/category\/big-mother\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/comments?post=3248"}],"version-history":[{"count":0,"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/posts\/3248\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/media?parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/categories?post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bloodhoundrealty.com\/BloodhoundBlog\/wp-json\/wp\/v2\/tags?post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}