Regular expression search multiple matches in PHP


/ Published in: PHP
Save to your folder(s)

This will search for specific type of tag in html doc and extract type attribute value and all attributes after it in tag.


Copy this code and paste it in your HTML
  1. $data = '<html>
  2.  
  3. <body>
  4. <jdoc:include type="head" name="popup" style="raw" />
  5.  
  6. <jdoc:include type="modules" name="dochead" style="raw" />
  7.  
  8. </body>
  9.  
  10. </html>';
  11.  
  12. if(preg_match_all('#<jdoc:include\ type="([^"]+)" (.*)\/>#iU', $data, $matches))
  13. {
  14. Will return
  15. $matches =
  16. [0][0] = "<jdoc:include type="head" name="popup" style="raw" />"
  17. [0][1] = "<jdoc:include type="modules" name="dochead" style="raw" />"
  18.  
  19. [1][0] = head
  20. [1][1] = modules
  21.  
  22. [2][0] = name="popup" style="raw"
  23. [2][1] = name="dochead" style="raw"
  24. }

Report this snippet


Comments


You need to login to view comments.