It was 4 days, I tried many different methods, now I almost refuse, it seems that something is impossible to do ...
I am trying to create a script where it will generate html text and put it in a blog post after it counts x number of words, let it say after it counts 10 words. I was able to achieve this with regex (split), but regex robs my post of HTML. In theory, this is something really simple, but I donβt know why it is so difficult in practice. I can not use paragraphs or any elements as links, it should not insert html only after the text, but not inside the code tags, for example, I have <script> some code </script> My blog text --- The text that needs to be inserted -- , it cannot calculate what is inside the script tags, it should only read plain text and insert a new html after it and be displayed. Say the same logic for entering text and inserting images using the WYSIWG editor. This is very difficult to explain.
Basically, I need something that just get a banner and insert it after x number of words in the text, just that.
Here is what I last tried, without any success (the code does not work):
<div style="width:1000px; margin-left:auto; margin-right:auto" class="newsitem_text"> <div style="width:980px; margin-left:auto; margin-right:auto"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pellentesque urna eu pulvinar maximus. Sed elit nunc, vestibulum ut eros vitae, pellentesque rhoncus ipsum. In et metus non diam porttitor maximus iaculis nec lectus. Quisque sodales scelerisque auctor. Nam rutrum venenatis eros, eu condimentum erat placerat ut. Pellentesque sed tempus sem, eu viverra ipsum. Vestibulum nec turpis convallis, dapibus massa vitae, posuere mauris. Suspendisse mattis tincidunt lorem. Aliquam erat volutpat. Nullam at tincidunt erat, maximus laoreet ipsum. </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> jQuery(function ($) { var wordList = $(".newsitem_text").html(); var newHtml = ' '; $.each(wordList, function(index, word) { newHtml += ' ' + word; if (index == 2) { newHtml += '<img src="https://www.google.com.br/logos/doodles/2015/adolphe-saxs-201st-birthday-6443879796572160.2-res.png" />' } }); $(newHtml).html('').insertAfter(".newsitem_text"); }); </script>
UPDATE is when I started to open threads in stackoverflow about this topic, I will give links for reference: