Use shared styles to replace consecutive pseudo-tags with HTML tags
P粉926174288
2023-09-01 18:44:45
<p>I have this line</p>
<pre class="brush:php;toolbar:false;">a[link], a[link] a [link] text text text a [link] text a[link] text</pre>
<p>So I want to find the first link before the text and do one operation on them and highlight them in a special style (in this case, maybe more or less three) and find the other links after the text Text and highlight them in different styles. </p>
<p>I could only find the first three links, but I don’t know how well I did</p>
<pre class="brush:php;toolbar:false;"><?php
$re = '/^(a\[(\w [\s ]?) \],?\s?) /iu';
$str = 'a[link], a[link] a[link] text text text a[link] text a[link] text';
preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0);
var_dump($matches);
?></pre>
<hr />
<p>I will now try to give an illustrative example of what is required:
There is such a paragraph</p>
<blockquote>
<p>a[link1], a[link2] a[link3] text text text a[link4] text a[link5] text</p>
</blockquote>
<p>In this article, there are links designated <code>a[...]</code>. In the future, I need to replace these links and change them to this form: </p>
<blockquote>
<p><a href="link1" class="style1">link1</a><a href="link2" class="style1">link2</a><a href=" link3" class="style1 ">link3</a> text text text<a href="link4" class="style2">link4</a> text<a href="link5" class="style2 ">link5</a> text</ p>
</blockquote>
<p>The first three linked classes are assigned the value <code>style1</code>. The link following the text already has a class value assigned to <code>style2</code>. </p>
<p>Initially, there can be three links in front of the text, four or even one, and there can be any number of links in any order after the text. </p>
Don't try to match everything at once. Match each link individually, then iterate over the results. To do this, use
preg_match_all
; if you want to replace on every match, usepreg_replace_callback
. use:should achieve your goal.
It's not clear what the goal of
. Also unclear about the optional comma and space after the link. Keeping it simple is the best approach.
[\s ]?
is, you can choose to allow spaces orhttps://3v4l.org/2AvT1