1

I've been trying to replace code-tags with divs that contain the classes I need.

I use JS to create the tags with a press of a button, this works fine, and I'm able to insert the tags as many times as I'd like.

code.addEventListener("click", function() {
    let val = contentBox.value + "\[code\]\[/code\]"  
    contentBox.value = val;
});

The problem only occurs when I'm trying to have php replace the tags.

link to image (can't post images seeing that I've yet to achive 10 reputation) As you can see in the picture, the returned regex is completely off. I've read about preg_* functions and I've used regex101 to look at the regex I parse. I'm not really sure what I've done wrong as I'm really new to regex.

<form method="post">
    <input type="text" name="title" placeholder="title" required> <br>
    <input type="file" name="postimage" placeholder="postimage" ><br>
    <input type="text" name="category" placeholder="category" required><br>
    <input type="button" id="code">Code</button>
    <input type="button" id="link">Link</button>
    <input type="button" id="test">Test</button>
    <textarea required placeholder="content" name="content" id="contentBox" style="height: 100px; width: 100%; resize: none;"></textarea>
    <input type="submit" value="submit" name="submit">
</form>
if(isset($_POST["submit"])) {
    $content = $_POST["content"];
    $open = preg_replace('/\[code\]/i', '<div class="code">', $content);
    $close = preg_replace('/\[\/code\]/i', "</div>", $content);
    $code = preg_split('/\[code\](.*?)\[\/code\]/m', $content);
}

I expect the output to have replaced the code-tags with the correct divs. And to ignore everything outside the code-tags.

Hope this all made any sense, if I've forgotten to include anything that might come in handy I'll do that upon request.

Muderino
  • 66
  • 8
  • So... I'm going to be "that guy" and point out that anyone submitting `[/code][/code][/code]` probably just broke your entire site. – Niet the Dark Absol Jul 13 '19 at 13:31
  • Yes, but the point of this tool is for people to write articles on their own sites(i.e portfolios). I couldn't imagine they would do that to themselves, but seeing that this could be a potential problem, I'll try and get around the edge cases. – Muderino Jul 13 '19 at 13:34
  • One "easy" way is to use the `$count` parameter of the replacement functions, that will tell you how many replacements were done. If they don't match, stop. – Niet the Dark Absol Jul 13 '19 at 13:43
  • That is excellent advice. I will look into it. Cheers! – Muderino Jul 13 '19 at 13:46

1 Answers1

0

So, seeing that I'm extremely slow... I just realized that I'd over complicated the entire thing and the solution was to simply use this

$content = preg_replace('/\[code\]/s', "<div class='code'>", $content);
$content = preg_replace('/\[\/code\]/s', "</div>", $content);
Muderino
  • 66
  • 8
  • Instead of preg_replace I think a str_replace might also work. You can omit the s in `/s`. It will make the dot match a newline but there are no dots in the pattern. – The fourth bird Jul 12 '19 at 13:07
  • 1
    Excellent, I will do so. Will also look into str_replace. Thanks for taking your time :) – Muderino Jul 12 '19 at 13:26