I have a string as this one: "[dr]azAZ09-09[/dr]"
Example: [dr]my name here-25[/dr]
I want to get all the elements:
my name here
-
25
How can I write the preg_split()
expression (or another function)?
It is unclear whether you need to do a simple preg_replace()
or preg_replace_callback()
(because you "need to do other things in the php code").
The best solution that I can offer you now is to use this pattern that requires [dr]
|[/dr]
tagging and a -
substring delimiter followed by one or more digits.
Pattern: (Demo)
# ⬐⬐⬐------------⬐⬐⬐⬐--------------- match opening and closing tags
# ↓↓↓↓↓ ↓↓↓↓↓↓↓
~\[dr](.+?)-(\d+)\[/dr]~
# ↑↑↑ ⬑⬑⬑---------------------- capture one or more digits
# ⬑⬑----------------------------- capture one or more of any character
With these two captured substrings, you can use preg_replace()
to generate a new dynamic replacements string.
Code: (Demo)
$bbcode = '[dr]my name here-25[/dr]';
echo preg_replace('~\[dr](.+?)-(\d+)\[/dr]~', '<div>Reply to: <a href="user.php?id=$2">$1</a></div>', $bbcode);
Output:
<div>Reply to: <a href="user.php?id=25">my name here</a></div>