0

I have preg_matches running. It works as follows:

  • Search for starting tag
  • Search for ending tag

This works; however, the page that I get data back from sometimes does not have data for that tag field. So instead of what should be a normal

<Field1>Data Here</Field1>

shows up as

<Field/>

So as you can see above, if there is no data (rather than not show the tag) it puts one ending tag and changes the tag itself too. Unfortunately, I need to enter "NA" for that data which may or may not be present.

(Note the <Field/>  Not </Field>.  

I'm curious to any thoughts you might have on being able to accomplish a workaround.

* Search for <field></field> 
* Also search for ></field>  
* Replace ></field>  with <field></field>  to match it all up.

Here is what I am using currently:

if(preg_match_all('@<(TicketNbr|Summary|Resolution|Site_Name|date_entered|status_description
|ServiceType)>\\s*(.*?)\\s*</\\1>@is', $resp, $m) ) {

so I figured I could go right into possibly a preg_realace which I believe will work like match_all just replacing them.

Will a preg_replace work against the above preg_match_all or if I could just tie in a

><field/>

into the preg_match_all.

  • 1
    Don't use regex for this; use a parser. You're running into the kinds of problems described in [this classic answer](http://stackoverflow.com/a/1732454/2057919). – elixenide Mar 17 '14 at 18:46
  • Please [don't parse XML](http://elegantcode.com/2010/08/07/dont-parse-that-xml/) ! – svvac Mar 17 '14 at 18:48
  • haha Ed, thanks. I saw that one but I have seen a lot where people seemed to get it to work. Looks like I will have to come up with a different solution. I think I'll take my previous code which ended up being limiting but had the data I needed and just put it into the updated code I have with the array compares etc.. lol Thanks though. – user3417137 Mar 17 '14 at 18:49
  • `` is perfectly valid syntax, and so are spaces inside tags. I think you need to be using a standards-compliant XML parser. – Sammitch Mar 17 '14 at 18:49
  • @swordofpain I hear you.. I wish this were able to be queried from a database or even if the report tool would actually work.. It is a task I am stuck with lol. – user3417137 Mar 17 '14 at 18:50
  • @Sammitch - Correct that is valid.. but through the report url, it should either be both tags or no tag .. would make life much easier. No worries.. I have a game plan. I appreciate the feedback though. – user3417137 Mar 17 '14 at 18:52
  • PHP's [DOM](http://fr2.php.net/manual/en/book.dom.php) to the rescue ! – svvac Mar 17 '14 at 18:53
  • @swordofpain - yeah.. but the page is not a valid XML page.. the tags are but it returns style information errors. Just another monkey wrench. – user3417137 Mar 17 '14 at 18:57
  • Well, that's certainly trickier... Depending on the extent of the damage, there are a number of solutions (sometimes hacks) to handle this, but I can't really help you without more to go on – svvac Mar 17 '14 at 19:04
  • @swordofpain - Yeah .. that's what I've been doing but then I just come across another bridge and this was the final hurdle but oddly enough between my other attempt and this attempt I have both pieces I need and can put them together.. I just figured a preg_replace may be easier and save time but in the end I think it will work out. – user3417137 Mar 17 '14 at 19:29

0 Answers0