正则表达式(将br和dash转换为li,或查找br标签之间的dash)

huangapple go评论80阅读模式
英文:

regex (convert br and dash to li, or find dash between br tag)

问题

如何将“-”转换为 PHP 中的列表(li)?示例 HTML:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
    任何文本 - 任何文本  任何文本  任何文本  任何文本  任何文本 
    <br><br>- 任何文本  任何文本<br>- 任何文本 1 2 3 | \ ,<br><br>
    <br>任何文本
<!-- end snippet -->

我尝试使用以下正则表达式:

(<\s*[^>]*>-\s?(.*?)<\s*[^>]*>)

但只能找到第一个匹配项...

通过这个正则表达式可以找到我需要的一切 - 从<br>标签开始,但它无法设置搜索的结束(结束标记将在<br>标签中):

<br>\s?-\s?(.*?)

(链接到测试: https://regex101.com/r/CqjpzX/1)。

英文:

How can convert the "-" to list (li) on PHP? Example HTML:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

any text - any text  any text any text  any text any text 

&lt;br&gt;&lt;br&gt;- any text any text&lt;br&gt;- any text 1 2 3 | \ ,&lt;br&gt;&lt;br&gt;
&lt;br&gt;any text

<!-- end snippet -->

I tried using the following regex:

(&lt;\s*[^&gt;]*&gt;-\s?(.*?)&lt;\s*[^&gt;]*&gt;)

But then only the first match can be found...

By this can find everything what I need - that start from the &lt;br&gt; tag, but it cannot set the end of the search (it will be in the &lt;br&gt; tag):

&lt;br&gt;\s?-\s?(.*?) (link to test: https://regex101.com/r/CqjpzX/1 ).

答案1

得分: 0

I'm not 100% sure this is what you're asking for, but if you want to capture everything between the <br>- and <br> you need to use a regex like this.
<br>\s?-\s?(.*?)(?=<br>)

Here is an example on your text https://regex101.com/r/71cHQB/1

I hope this can help you.

Also don't forget the /g after the regex so it doesn't stop after the first match.

英文:

I'm not 100% sure this is what you're asking for, but if you want to capture everything between the &lt;br&gt;- and &lt;br&gt; you need to use a regex like this.
&lt;br&gt;\s?-\s?(.*?)(?=&lt;br&gt;)

Here is an example on your text https://regex101.com/r/71cHQB/1

I hope this can help you.

Also don't forget the /g after the regex so it doesn't stop after the first match.

huangapple
  • 本文由 发表于 2020年1月3日 20:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定