Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
构建正则表达式以在php字符串中的li标记中放置子ul?_Php_Regex - Fatal编程技术网

构建正则表达式以在php字符串中的li标记中放置子ul?

构建正则表达式以在php字符串中的li标记中放置子ul?,php,regex,Php,Regex,我是新加入RegEx的,我正在寻找最简单的方法,将li添加到另一个ul的一些孩子身上。我得到的例子是: 我想: 我尝试了几种方法,但我的主要问题是在regex中输入替换只发生在我尝试使用preg_替换的另一个ul的ul孩子身上 你有什么想法或建议吗 非常感谢你的帮助 尼古拉斯试试这个: 正则表达式解释: 如果你想花最少的时间制作一个能工作的原型,你可以使用正则表达式。但是,如果你想在长期内用最少的时间把头撞在墙上,那就别管它了,改用DOM操纵。+1表示DOM,而不是regex。好了,伙计们,非常

我是新加入RegEx的,我正在寻找最简单的方法,将li添加到另一个ul的一些孩子身上。我得到的例子是:

我想:

我尝试了几种方法,但我的主要问题是在regex中输入替换只发生在我尝试使用preg_替换的另一个ul的ul孩子身上

你有什么想法或建议吗

非常感谢你的帮助

尼古拉斯试试这个:

正则表达式解释:


如果你想花最少的时间制作一个能工作的原型,你可以使用正则表达式。但是,如果你想在长期内用最少的时间把头撞在墙上,那就别管它了,改用DOM操纵。+1表示DOM,而不是regex。好了,伙计们,非常感谢。我试过你的解决方案,塞利安,但由于某种原因它不起作用。。。我更喜欢使用DOM,但如何替换用于解析的标记,即ul。我可以问你一个例子吗?干杯@尼古拉斯:我已经更新了我的解决方案。现在可以了吗?请。。甚至修改HTML。DOM引擎的工作做得非常好!
$result = preg_replace('%(?s)(?<=</li>)(\s*<ul>.+?</ul>)%im', '<li>$1</li>', $subject);
<!--
(?is)(?<=</li>)(\s*<ul>.+?</ul>)

Options: case insensitive; ^ and $ match at line breaks

Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s) «(?is)»
Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=</li>)»
   Match the characters “</li>” literally «</li>»
Match the regular expression below and capture its match into backreference number 1 «(\s*<ul>.+?</ul>)»
   Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match the characters “<ul>” literally «<ul>»
   Match any single character «.+?»
      Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
   Match the characters “</ul>” literally «</ul>»
-->