get wrapping element using preg_match php

前端 未结 3 1105
无人共我
无人共我 2021-01-24 16:31

I want a preg_match code that will detect a given string and get its wrapping element. I have a string and a html code like:

$string = \"My text\";
$html = \"<         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-24 16:58

    Even if regex is never the correct answer in the domain of dom parsing, I came out with another (quite simple) solution

    <[^>/]+?>My String
    

    if the html is good (ie it has closing tags, < is replaced with < & so on). This way you have in the first regex group the opening tag and in the second the closing one.

提交回复
热议问题