1

私はRegExを初めて使用し、別のulのいくつかの子ulの周りにliを追加する最も簡単な方法を探しています。私が得た例:

そして、私はしたいです:

私はいくつかの方法を試しましたが、私の主な問題は、置換が別の ul の ul 子のみに発生することを正規表現に入れることです (私は preg_replace を使用しようとしました)。

アイデアや提案はありますか?

助けてくれて本当にありがとうございます、

ニコラス

4

1 に答える 1

0

これを試して:

$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 “&lt;/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 “&lt;ul>” literally «<ul>»
   Match any single character «.+?»
      Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
   Match the characters “&lt;/ul>” literally «</ul>»
-->
于 2012-05-08T10:21:38.727 に答える