I have this line
a[link], a[link] a [link] text text text a [link] text a[link] text
So I want to find the first links before the text and do one operation with them and highlight them in a special style (in this example, there may be three of them more or less) and find other links that go after the text and highlight them differently in styles.
I was able to find only the first three links, but I don't know how well I did it
<?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);
?>
Don't try to match everything at once. Match each link individually then iterate over the results. Usepreg_match_all
for this, orpreg_replace_callback
if you want to do replace on each match. Using:
a\[(\w+)\]
should achieve your goal.
It was unclear what the[\s+]?
goal was, that optionally would allow whitespaces or+
s. Also unclear about the optional comma and space after the links. Keeping it simple is the best approach.
With php you can use the\G
anchor and then use 2 capture groups to know the difference between the lines at the beginning and the other links.
\Ga\h*\[([^][]*)],?\h*\[([^][]*)]
Explanation
\G
Assert the current position at the start of the string, or at the end of the previous matcha\h*
Matcha
and optional horizontal whitespace chars\[([^][]*)]
Match[...]
and capture in group 1 what is in between the square brackets,?\h*
match an optional comma and horizontal whitespace chars
Or\[([^][]*)]
Match[...]
and capture in group 2 what is in between the square bracketsSee a regex demo.
I will try now to give an illustrative example of what is needed: There is such a text
a[link1], a[link2] a[link3] text text text a[link4] text a[link5] text
in this text there are links designated a[ ] in the future I need to replace these links and bring it to this form
<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
The first three links have a class assigned with the value style1 the links that come after the text already have a class value assigned to style2 At the very beginning, there can be three links before the text, four or even one, as well as after the text there can be any number of links in any order.
Thank you all so much for your help. I kind of figured it out. First, I use regular expressions to search for all the links at the beginning of the text, then I run the result through str_replace and change a[ ] to what I need, and at the end I just glue the result through concatenation and output it to the screen.
Usepreg_replace_callback()
to capture and replace the desired strings in one consolidated process.
Use a lookbehind containing a closing square brace in the first capture group to differentiate between the first match of a continuous series versus a subsequent member of the same continuous series.
The first capture group ($m[1]
) will be null or an empty string
The second capture group ($m[2]
) will be the glue characters between continuous links.
The third capture group ($m[3]
) will be the link's targeted text.
Every time you encounter a first-in-group link (null lookbehind), increment the style counter.
Code: (Demo)
$styleCounter = 0;
echo preg_replace_callback(
'/((?<=]))?+(,? ?)a\[([^][]*)]/',
function ($m) use(&$styleCounter) {
if ($m[1] === null) {
++$styleCounter;
}
return "{$m[2]}<a href=\"{$m[3]}\" class=\"style{$styleCounter}\">{$m[3]}</a>";
},
$string,
-1,
$count,
PREG_UNMATCHED_AS_NULL
);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.