Jump to content
Sign in to follow this  
MrFish

Is this regular expression possible?

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Yeah. If you're serious about this, use DOMDocument. It sounds like you know what you're doing anyway.

Share this post


Link to post
Share on other sites

I went ahead and used DOMDocument. Thanks for the advice from everyone.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...