Search code examples
phpregexpodio

Find the nth instance of a tag in XML document from Globiflow


I'm unable to use preg_match_all() in Globiflow (for Podio) due to their insistence on preg_match_gf(), which has similar functionality as the regular preg_match(). I have the following code:

preg_match_gf("/<zestimate><amount currency=\"USD\">(.*?)
<\/amount>/ism",*search_result_below*, 1)

and I have it searching the following information for some random property (that I've greatly simplified):

<comparables>
 <comp score="5.0">
  <zpid>########</zpid>
   <zestimate>
    <amount currency="USD">832447</amount>
   </zestimate>
  </comp>
 <comp score="11.0">
  <zpid>########</zpid>
   <zestimate>
    <amount currency="USD">526855</amount>
   </zestimate>
  </comp>
 <comp score="2.0">
  <zpid>########</zpid>
   <zestimate>
    <amount currency="USD">709637</amount>
   </zestimate>
  </comp>
 <comp score="6.0">
  <zpid>########</zpid>
   <zestimate>
    <amount currency="USD">607666</amount>
   </zestimate>
  </comp>
 <comp score="8.0">
  <zpid>########</zpid>
   <zestimate>
    <amount currency="USD">631700</amount>
   </zestimate>
  </comp>
</comparables>

I want to be able to select each instance of the <amount currency="USD"> for each property, as they're going to fill their own fields via their own calculations. The <comp score="#.0"> changes with each query, as do the line positions. I'm unable to use print or echo, as Globiflow considers them illegal operators.


Solution

  • You can also use preg_match_all_gf to get each amount, eg:

    preg_match_all_gf('/<amount currency="USD">(.*?)<\/amount>/ism', [xml-token],1)
    preg_match_all_gf('/<amount currency="USD">(.*?)<\/amount>/ism', [xml-token],2)
    preg_match_all_gf('/<amount currency="USD">(.*?)<\/amount>/ism', [xml-token],3)
    

    etc