Jump to content

Is this regular expression possible?


MrFish

Recommended Posts

I've got an xml file I want to run a regular expression on. I want to load elements dynamically using an element I'm calling "c-load". These c-load elements can be nested and use the results from the previous c-load in its queries. Here is what my xml looks like-

<!-- Community Results Page --><panel name="com_results" class="GridMenuPanel">  <data columns="4" rows="3" icon-width="128" icon-height="170">   <c-load connection="main" table="community" order="community_order">	<option key="{community_name}" value="com_detail_{community_name}" image="http://site/CmImageDir/{community_logo}"/>   </c-load>  </data></panel> <!-- Community Detail Pages --><c-load connection="main" table="community" order="community_order">  <panel name="com_detail_{community_name}" class="SlidePanel">   <data>	<panel name="com_models_{community_name}"/>	<panel name="com_inventory_{community_name}"/>	<panel name="com_overview_{community_name}"/>   </data>  </panel></c-load> <!-- Community Models Pages --><c-load connection="main" table="community" order="community_order">  <c-load connection="main" query="SELECT * FROM community_model WHERE community_model_community_id='{community_id}' ORDER BY community_model_order ASC">   <panel name="com_modeldetal_{[1]community_name}_{model_name}" class="ModelResultsPanel">	<data>	 <option key="{model_name"} value="com_modedetail_{[1]community_name}_{model_name}">	  <_attribute name="{model_name}"/>	  <_attribute beds="{model_bed}"/>	  <_attribute baths="{model_bathfull}"/>	  <_attribute price="{model_baseprice}"/>	  <_attribute sqft="{model_sqft}"/>	 </option>	</data>   </panel>  </c-load></c-load>

So I could write an expression like this and it will work for most-

|<c-load(.*)>([\w\W\d\D]+)</c-load>|U

But if c-load elements are nested like what's seen in the Community Models Pages section it will end at the first </c-load> it finds. If I wrote a custom function to pull out c-load elements I would count how many nested <c-load> elements I found with a counter and each time I run into </c-load> I would decrease the counter. If the counter is 0 then the </c-load> found would be the end of the block.

<c-load> <!-- counter is 0 --> <c-load> <!-- counter += 1 --></c-load> <!-- counter != 0 - counter -= 1 --></cload> <!-- counter is 0 - return block -->

So is this possible using one line of regex or do I need to come up with a custom solution?

Edited by MrFish
Link to comment
Share on other sites

I think you'll be happier treating your xml as a DOMDocument or SimpleXML object instead of a string. Then you could use x-path to target the kind of nodes you want based on their attributes -- it looks like the one you want has unique attributes, anyway -e.g., table, order.

Link to comment
Share on other sites

I think you'll be happier treating your xml as a DOMDocument or SimpleXML object instead of a string. Then you could use x-path to target the kind of nodes you want based on their attributes -- it looks like the one you want has unique attributes, anyway -e.g., table, order.
I may try to convert this to DOMDocument. Unfortunately I started using SimpleXML without realizing it doesn't have an insertBefore method and order of elements is important in my script.
Link to comment
Share on other sites

The dom_import_simplexml() is all you need in case you want to keep your SimpleXML stuff at some place, and just switch to DOM for this one manipulation, and then use simplexml_import_dom() to switch back to SimpleXML.Of course, it will be more efficient if you always use DOM instead.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...