How to replace custom bbcode tag containing structured content?












-2















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)?










share|improve this question




















  • 1





    Does your string always start with [dr] and end with [/dr]?

    – Jeto
    Nov 10 '18 at 10:55






  • 1





    What defines "element"? For instance, what do you expect for [dx]a?})---7y3/!4[$[/dx]abcdef?

    – trincot
    Nov 10 '18 at 10:55








  • 1





    Can you include more sample data?

    – Tim Biegeleisen
    Nov 10 '18 at 10:58











  • @Lilia There are more than 10 different "correct" ways to do this. We simply don't know enough about your question requirements. Please explain how your input data may vary. Tell us more about the process logic so that we can be sure to offer you the best possible pattern. Is [dr]azAZ09-09[/dr] an example of an entire input string or might it be a substring of a much larger portion of text?

    – mickmackusa
    Nov 10 '18 at 12:33













  • I explain better: I extract several posts from the DB. If in the posts insertion textarea the user enters the following "bbcode": [dr] every thing - number [/dr] I must intercept this string and turn it into a quote, during the insert in the DB. Of course, the user can arbitrarily modify that string but I just have at least "-" and "number" because then I will need to do other things in the php code

    – Lilia
    Nov 10 '18 at 17:07


















-2















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)?










share|improve this question




















  • 1





    Does your string always start with [dr] and end with [/dr]?

    – Jeto
    Nov 10 '18 at 10:55






  • 1





    What defines "element"? For instance, what do you expect for [dx]a?})---7y3/!4[$[/dx]abcdef?

    – trincot
    Nov 10 '18 at 10:55








  • 1





    Can you include more sample data?

    – Tim Biegeleisen
    Nov 10 '18 at 10:58











  • @Lilia There are more than 10 different "correct" ways to do this. We simply don't know enough about your question requirements. Please explain how your input data may vary. Tell us more about the process logic so that we can be sure to offer you the best possible pattern. Is [dr]azAZ09-09[/dr] an example of an entire input string or might it be a substring of a much larger portion of text?

    – mickmackusa
    Nov 10 '18 at 12:33













  • I explain better: I extract several posts from the DB. If in the posts insertion textarea the user enters the following "bbcode": [dr] every thing - number [/dr] I must intercept this string and turn it into a quote, during the insert in the DB. Of course, the user can arbitrarily modify that string but I just have at least "-" and "number" because then I will need to do other things in the php code

    – Lilia
    Nov 10 '18 at 17:07
















-2












-2








-2








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)?










share|improve this question
















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)?







php regex preg-replace bbcode






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '18 at 23:16









mickmackusa

22.8k103356




22.8k103356










asked Nov 10 '18 at 10:50









LiliaLilia

12




12








  • 1





    Does your string always start with [dr] and end with [/dr]?

    – Jeto
    Nov 10 '18 at 10:55






  • 1





    What defines "element"? For instance, what do you expect for [dx]a?})---7y3/!4[$[/dx]abcdef?

    – trincot
    Nov 10 '18 at 10:55








  • 1





    Can you include more sample data?

    – Tim Biegeleisen
    Nov 10 '18 at 10:58











  • @Lilia There are more than 10 different "correct" ways to do this. We simply don't know enough about your question requirements. Please explain how your input data may vary. Tell us more about the process logic so that we can be sure to offer you the best possible pattern. Is [dr]azAZ09-09[/dr] an example of an entire input string or might it be a substring of a much larger portion of text?

    – mickmackusa
    Nov 10 '18 at 12:33













  • I explain better: I extract several posts from the DB. If in the posts insertion textarea the user enters the following "bbcode": [dr] every thing - number [/dr] I must intercept this string and turn it into a quote, during the insert in the DB. Of course, the user can arbitrarily modify that string but I just have at least "-" and "number" because then I will need to do other things in the php code

    – Lilia
    Nov 10 '18 at 17:07
















  • 1





    Does your string always start with [dr] and end with [/dr]?

    – Jeto
    Nov 10 '18 at 10:55






  • 1





    What defines "element"? For instance, what do you expect for [dx]a?})---7y3/!4[$[/dx]abcdef?

    – trincot
    Nov 10 '18 at 10:55








  • 1





    Can you include more sample data?

    – Tim Biegeleisen
    Nov 10 '18 at 10:58











  • @Lilia There are more than 10 different "correct" ways to do this. We simply don't know enough about your question requirements. Please explain how your input data may vary. Tell us more about the process logic so that we can be sure to offer you the best possible pattern. Is [dr]azAZ09-09[/dr] an example of an entire input string or might it be a substring of a much larger portion of text?

    – mickmackusa
    Nov 10 '18 at 12:33













  • I explain better: I extract several posts from the DB. If in the posts insertion textarea the user enters the following "bbcode": [dr] every thing - number [/dr] I must intercept this string and turn it into a quote, during the insert in the DB. Of course, the user can arbitrarily modify that string but I just have at least "-" and "number" because then I will need to do other things in the php code

    – Lilia
    Nov 10 '18 at 17:07










1




1





Does your string always start with [dr] and end with [/dr]?

– Jeto
Nov 10 '18 at 10:55





Does your string always start with [dr] and end with [/dr]?

– Jeto
Nov 10 '18 at 10:55




1




1





What defines "element"? For instance, what do you expect for [dx]a?})---7y3/!4[$[/dx]abcdef?

– trincot
Nov 10 '18 at 10:55







What defines "element"? For instance, what do you expect for [dx]a?})---7y3/!4[$[/dx]abcdef?

– trincot
Nov 10 '18 at 10:55






1




1





Can you include more sample data?

– Tim Biegeleisen
Nov 10 '18 at 10:58





Can you include more sample data?

– Tim Biegeleisen
Nov 10 '18 at 10:58













@Lilia There are more than 10 different "correct" ways to do this. We simply don't know enough about your question requirements. Please explain how your input data may vary. Tell us more about the process logic so that we can be sure to offer you the best possible pattern. Is [dr]azAZ09-09[/dr] an example of an entire input string or might it be a substring of a much larger portion of text?

– mickmackusa
Nov 10 '18 at 12:33







@Lilia There are more than 10 different "correct" ways to do this. We simply don't know enough about your question requirements. Please explain how your input data may vary. Tell us more about the process logic so that we can be sure to offer you the best possible pattern. Is [dr]azAZ09-09[/dr] an example of an entire input string or might it be a substring of a much larger portion of text?

– mickmackusa
Nov 10 '18 at 12:33















I explain better: I extract several posts from the DB. If in the posts insertion textarea the user enters the following "bbcode": [dr] every thing - number [/dr] I must intercept this string and turn it into a quote, during the insert in the DB. Of course, the user can arbitrarily modify that string but I just have at least "-" and "number" because then I will need to do other things in the php code

– Lilia
Nov 10 '18 at 17:07







I explain better: I extract several posts from the DB. If in the posts insertion textarea the user enters the following "bbcode": [dr] every thing - number [/dr] I must intercept this string and turn it into a quote, during the insert in the DB. Of course, the user can arbitrarily modify that string but I just have at least "-" and "number" because then I will need to do other things in the php code

– Lilia
Nov 10 '18 at 17:07














1 Answer
1






active

oldest

votes


















0














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>





share|improve this answer


























  • now your pattern works. Only one thing: how can I get the "-number" part? Example: [dr]my name here-25[/dr] I need of 25

    – Lilia
    Nov 11 '18 at 14:13











  • Please better explain your process and include your coding attempt. I can easily solve what you are doing if I can see your desired outcome. I have a feeling that you want preg_replace_callback() .

    – mickmackusa
    Nov 11 '18 at 20:38











  • $search = array( '{[dr](.+?)-(d+)[/dr]}siU' ); $replace = array( '<div>Reply to: \2 [here I need a link to -NUMBER]</div>' ); $newvar = preg_replace($search, $replace, $newvar);

    – Lilia
    Nov 12 '18 at 17:49











  • So what will your exact desired output be, given your sample input? <div>Reply to: <a href="user.php?id=[thenumber]">[thenumber]</a></div>? or do you need to run a "lookup" and replace [thenumber] with some other value?

    – mickmackusa
    Nov 12 '18 at 23:15











  • the href tag you indicated would be perfect for me

    – Lilia
    Nov 13 '18 at 18:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238194%2fhow-to-replace-custom-bbcode-tag-containing-structured-content%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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>





share|improve this answer


























  • now your pattern works. Only one thing: how can I get the "-number" part? Example: [dr]my name here-25[/dr] I need of 25

    – Lilia
    Nov 11 '18 at 14:13











  • Please better explain your process and include your coding attempt. I can easily solve what you are doing if I can see your desired outcome. I have a feeling that you want preg_replace_callback() .

    – mickmackusa
    Nov 11 '18 at 20:38











  • $search = array( '{[dr](.+?)-(d+)[/dr]}siU' ); $replace = array( '<div>Reply to: \2 [here I need a link to -NUMBER]</div>' ); $newvar = preg_replace($search, $replace, $newvar);

    – Lilia
    Nov 12 '18 at 17:49











  • So what will your exact desired output be, given your sample input? <div>Reply to: <a href="user.php?id=[thenumber]">[thenumber]</a></div>? or do you need to run a "lookup" and replace [thenumber] with some other value?

    – mickmackusa
    Nov 12 '18 at 23:15











  • the href tag you indicated would be perfect for me

    – Lilia
    Nov 13 '18 at 18:39
















0














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>





share|improve this answer


























  • now your pattern works. Only one thing: how can I get the "-number" part? Example: [dr]my name here-25[/dr] I need of 25

    – Lilia
    Nov 11 '18 at 14:13











  • Please better explain your process and include your coding attempt. I can easily solve what you are doing if I can see your desired outcome. I have a feeling that you want preg_replace_callback() .

    – mickmackusa
    Nov 11 '18 at 20:38











  • $search = array( '{[dr](.+?)-(d+)[/dr]}siU' ); $replace = array( '<div>Reply to: \2 [here I need a link to -NUMBER]</div>' ); $newvar = preg_replace($search, $replace, $newvar);

    – Lilia
    Nov 12 '18 at 17:49











  • So what will your exact desired output be, given your sample input? <div>Reply to: <a href="user.php?id=[thenumber]">[thenumber]</a></div>? or do you need to run a "lookup" and replace [thenumber] with some other value?

    – mickmackusa
    Nov 12 '18 at 23:15











  • the href tag you indicated would be perfect for me

    – Lilia
    Nov 13 '18 at 18:39














0












0








0







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>





share|improve this answer















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>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 20:03

























answered Nov 10 '18 at 23:10









mickmackusamickmackusa

22.8k103356




22.8k103356













  • now your pattern works. Only one thing: how can I get the "-number" part? Example: [dr]my name here-25[/dr] I need of 25

    – Lilia
    Nov 11 '18 at 14:13











  • Please better explain your process and include your coding attempt. I can easily solve what you are doing if I can see your desired outcome. I have a feeling that you want preg_replace_callback() .

    – mickmackusa
    Nov 11 '18 at 20:38











  • $search = array( '{[dr](.+?)-(d+)[/dr]}siU' ); $replace = array( '<div>Reply to: \2 [here I need a link to -NUMBER]</div>' ); $newvar = preg_replace($search, $replace, $newvar);

    – Lilia
    Nov 12 '18 at 17:49











  • So what will your exact desired output be, given your sample input? <div>Reply to: <a href="user.php?id=[thenumber]">[thenumber]</a></div>? or do you need to run a "lookup" and replace [thenumber] with some other value?

    – mickmackusa
    Nov 12 '18 at 23:15











  • the href tag you indicated would be perfect for me

    – Lilia
    Nov 13 '18 at 18:39



















  • now your pattern works. Only one thing: how can I get the "-number" part? Example: [dr]my name here-25[/dr] I need of 25

    – Lilia
    Nov 11 '18 at 14:13











  • Please better explain your process and include your coding attempt. I can easily solve what you are doing if I can see your desired outcome. I have a feeling that you want preg_replace_callback() .

    – mickmackusa
    Nov 11 '18 at 20:38











  • $search = array( '{[dr](.+?)-(d+)[/dr]}siU' ); $replace = array( '<div>Reply to: \2 [here I need a link to -NUMBER]</div>' ); $newvar = preg_replace($search, $replace, $newvar);

    – Lilia
    Nov 12 '18 at 17:49











  • So what will your exact desired output be, given your sample input? <div>Reply to: <a href="user.php?id=[thenumber]">[thenumber]</a></div>? or do you need to run a "lookup" and replace [thenumber] with some other value?

    – mickmackusa
    Nov 12 '18 at 23:15











  • the href tag you indicated would be perfect for me

    – Lilia
    Nov 13 '18 at 18:39

















now your pattern works. Only one thing: how can I get the "-number" part? Example: [dr]my name here-25[/dr] I need of 25

– Lilia
Nov 11 '18 at 14:13





now your pattern works. Only one thing: how can I get the "-number" part? Example: [dr]my name here-25[/dr] I need of 25

– Lilia
Nov 11 '18 at 14:13













Please better explain your process and include your coding attempt. I can easily solve what you are doing if I can see your desired outcome. I have a feeling that you want preg_replace_callback() .

– mickmackusa
Nov 11 '18 at 20:38





Please better explain your process and include your coding attempt. I can easily solve what you are doing if I can see your desired outcome. I have a feeling that you want preg_replace_callback() .

– mickmackusa
Nov 11 '18 at 20:38













$search = array( '{[dr](.+?)-(d+)[/dr]}siU' ); $replace = array( '<div>Reply to: \2 [here I need a link to -NUMBER]</div>' ); $newvar = preg_replace($search, $replace, $newvar);

– Lilia
Nov 12 '18 at 17:49





$search = array( '{[dr](.+?)-(d+)[/dr]}siU' ); $replace = array( '<div>Reply to: \2 [here I need a link to -NUMBER]</div>' ); $newvar = preg_replace($search, $replace, $newvar);

– Lilia
Nov 12 '18 at 17:49













So what will your exact desired output be, given your sample input? <div>Reply to: <a href="user.php?id=[thenumber]">[thenumber]</a></div>? or do you need to run a "lookup" and replace [thenumber] with some other value?

– mickmackusa
Nov 12 '18 at 23:15





So what will your exact desired output be, given your sample input? <div>Reply to: <a href="user.php?id=[thenumber]">[thenumber]</a></div>? or do you need to run a "lookup" and replace [thenumber] with some other value?

– mickmackusa
Nov 12 '18 at 23:15













the href tag you indicated would be perfect for me

– Lilia
Nov 13 '18 at 18:39





the href tag you indicated would be perfect for me

– Lilia
Nov 13 '18 at 18:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238194%2fhow-to-replace-custom-bbcode-tag-containing-structured-content%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation