Search code examples
phphtmlmarkup

PHP Custom Markup Language Parser


I am making a website, and I would like to make a custom Markup type language in PHP. I want the tags to be surrounded with [ and ]. Now, I was thinking about this, like anyone would, and I could do something like this:

function formatMarkup($markup = ''){
    $markup = str_replace('[color=blue]', '<span style="color: blue">', $markup);
    return $markup
}

Even though that might work, it would be more progrematically correct if it would do something like explode(), but starting at every [ and ending at every ]. This would be great if I found out. Thank you for your time and effort.

EDIT: I have decided to use preg_split(). It seems nice, and all, but I cannot get the regex. Here is my code.

EDIT #2: I have got most of the regex done, but there are uneeded extra keys in the array. How would I fix them? Here is my new code.


Solution

  • I have made my Markup language. I used

    $split = preg_split("/(\[|\])/", $markup);
    

    to get the individual "tags" and used

    foreach($split as $k => $v){
            if(strlen($v) < 1){
                continue;
            }
    

    to illiterate through each of them, and check if the value is empty. Then, after that, I would do all of my checks, and parse the code blocks together, and make line, after line, the re-constructed text.