specifics replace substring in string with php function -


In the lesson, I would like to change the div tag li . But not everyone is just some well defined. In this case, people who have the ID "tab- *" starts, I need to use the PHP function easily:

  & lt; Div id = "tab-141285" class = "my-class" & gt; & Lt; Div class = "my-subclass" & gt; & Lt; Div & gt; Advertising Ready Advertising & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div id = "tab-85429" class = "my-class" & gt; & Lt; Div class = "my-subclass" & gt; & Lt; Div & gt; Advertising Ready Advertising & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt;   

Get this text

  & lt; Li id = "tab-141285" class = "my-class" & gt; & Lt; Div class = "my-subclass" & gt; & Lt; Div & gt; Advertising Ready Advertising & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Li & gt; & Lt; Li id = "tab-85429" class = "my-class" & gt; & Lt; Div class = "my-subclass" & gt; & Lt; Div & gt; Advertising Ready Advertising & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Li & gt;   

Can you advise me?

Thanks

Regular expressions for parsing HTML are not sufficient I Any regex trying to use will be fragile. Instead, I suggest this.

This idea is:

  1. all & lt; Div & gt; Find elements that start with a id attribute "tab -" // div [starts (@id, " Tab - ")]
  2. For each of them, a new & lt; Li & gt; element.
  3. All & lt; Div & gt;
  4. Li> >.

    Because there is no basic element in your string, we will dance a little bit before we put it in one and then make it again.


    Example:
      $ html = & lt; & Lt; & Lt; 'Html' & lt; Div id = "tab-141285" class = "my-class" & gt; & Lt; Div class = "my-subclass" & gt; & Lt; Div & gt; Advertising Ready Advertising & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div id = "tab-85429" class = "my-class" & gt; & Lt; Div class = "my-subclass" & gt; & Lt; Div & gt; Advertising Ready Advertising & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; Html; $ Dom = new DOMDocument (); $ Dom- & gt; Load HTML ("& lt; div> $ html ", LIBXML_HTML_NOIMPLIED); $ Xpath = new DOMXPath ($ dom); $ Node = $ xpath- & gt; Query ('// div [Start-start ('id,' tab- "]] '); Foreign currency ($ node $ as node) {$ li = $ dom- & gt; Create element (' li ' ); While ($ node-> attribute-> length) {$ li-> SetAttiInode ($ node-> Attribute-> Item (0));} while ($ node- & First Child) {$ li-> Epand Chલ્ડ ($ node-> First Child);} $ Node-> parentNode-> Infant ($ li, $ node);} $ Html = ''; Foreign currency ($ dom- & gt; document element-> child nodes $ node form) {$ html. $ = $ Dom-> Save HTML ($ node);} $ Html echo; < / Code>  

    OU Put:
      
  5. li id ​​= "tab-141285" class = "my-class">
    & gt; Lt; div & gt; Ads Ready for Advertising & lt; / div & gt; & lt; / div & gt; & lt; / li & gt; & lt; li id ​​= "tab-85429" class = "My-class">
    Ad ready to ad & lt; / div & gt; & lt; / div & gt;

    Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -