-7

In my project, I have a form. Please see below:

enter image description here

here, I have two fields, HTMl and CSS. User can save their template by saving this form. Currently, I saved HTML and css as a separate field in my database. I need to prepare a template with valid data. So, I fetch the HTML from database. Please see the questions below: *** When I fetch the HTML. It's a string.

  1. I need to catch two numbers(1 and 5) from #{item(1, 5)} using regular expression. what will be the pattern?

  2. I need to catch <div class="layout">....</div> block by using regular expression. What will be the pattern?

  3. I need to replace #{url} with valid data by using regular expression. What will be the pattern?

Here is HTML with custom tag:

#{if(total,0)}#{else}
<div id="recommend">
  <div class="title"><p>Title</p></div>
    #{item(1,5)}
  <div class="layout">
    <div class="item">
      <a href="#{url}"><img border="0" alt="#{name}" src="#{image}"></a>
    </div>
    <div class="goods">
       <a href="#{url}">#{truncate(name,27)}</a>
    </div>
    <div class="price">#{comma(price_tax)}</div>
  </div>
  #{/item}
  <br clear="all">
</div>
#{/if}

Please help me.

  • 2
    [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/a/1732454/3000179) – ʰᵈˑ Mar 02 '16 at 08:41
  • [Should “Give me a regex that does X” questions be closed?](http://meta.stackoverflow.com/questions/285733/should-give-me-a-regex-that-does-x-questions-be-closed/285739#285739) – Wiktor Stribiżew Mar 02 '16 at 08:43
  • @Khaled Saiful Islam: http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php you can follow this. It may help you – SkyWalker Mar 02 '16 at 08:45

1 Answers1

0
  1. #{[\w]+\((\d+),(\d+)\)}

https://regex101.com/r/vS0uJ2/1

Output:

MATCH 1  
1.  [94-95] `1`  
2.  [96-97] `5`  
  1. <div class="layout"\>((?:.*?(?:<div).*?(?:<\/div>).*?){0,})<\/div>

https://regex101.com/r/bL8gY3/2

Output:

MATCH 1
1.  [102-371]   
`<div class="layout">
    <div class="item">
      <a href="#{url}"><img border="0" alt="#{name}" src="#{image}"></a>
    </div>
    <div class="goods">
       <a href="#{url}">#{truncate(name,27)}</a>
    </div>
    <div class="price">#{comma(price_tax)}</div>
  </div>`
  1. <a href="(#\{url\})

https://regex101.com/r/aB2sH1/1

Output:

MATCH 1
1.  [161-167]   `#{url}`
MATCH 2
1.  [270-276]   `#{url}`
Tim007
  • 2,557
  • 1
  • 11
  • 20