0

I need to inject some html into a text while preserving the original html. It doesn't matter if it's in javascript or php.

As an example. Base Test:

Die geis­tes– und sozi­al­wis­sen­schaft­li­che
Aus­ein­an­der­set­zung mit <em>Human Enhan­ce­ment</em> hat erneut zu
Debat­ten über den Sta­tus des Men­schen als Cyborg geführt.

Then I got the same thing as html-stripped string. Search String:

Die geis­tes– und sozi­al­wis­sen­schaft­li­che
Aus­ein­an­der­set­zung mit Human Enhan­ce­ment hat erneut zu
Debat­ten über den Sta­tus des Men­schen als Cyborg geführt.

I can search by the second string, but need to inject some html behind the first one. Result:

Die geis­tes– und sozi­al­wis­sen­schaft­li­che
Aus­ein­an­der­set­zung mit <em>Human Enhan­ce­ment</em> hat erneut zu
Debat­ten über den Sta­tus des Men­schen als Cyborg geführt.<span>1</span>

It's not that easy ... :) I can find and replace the string via Regex and jQuery's .text() function. But that doesn't preserve the original html.

thgie
  • 2,367
  • 1
  • 18
  • 28
  • Why not use `jQElem.append()` if you're using jquery or `document.querySelector().innerHTML += text` if you're not using jQuery ? – Lauromine Aug 25 '15 at 09:37
  • Because the found text can be inside a

    in example. I need the to be exactly behind the found text, not the html element.

    – thgie Aug 25 '15 at 09:46
  • Something like this? http://stackoverflow.com/questions/31275446/how-to-wrap-part-of-a-text-in-a-node-with-javascript – nhahtdh Aug 25 '15 at 10:05

3 Answers3

0

php: strpos + length of your needle to get the end position of it in a string as $index, then substr_replace($string, $insertion, $index, 0);

Auris
  • 1,309
  • 1
  • 9
  • 18
0

I could solve the problem by modifiny the search sentence with regex. Replaced all spaces with

\s*?(?:<\/?[^>]*?>)?\s*?

Example: https://regex101.com/r/nI0vY7/1

thgie
  • 2,367
  • 1
  • 18
  • 28
0
<?php
$string = "I want to add before this element";
function Add($add, $before, $in_content)
    {
        $p = strpos($in_content, $before, 0);
        if ($p !== false)
           return substr_replace( $in_content , $add , $p, 0 );
        else
           return false;
    }

    $result = Add("paragraph ","this",$string);
    if($result !== false)
        echo $result;
?>  

Do you need to get the text from html dom or from database or file?

Javascript:

<div id="content">I want to add before this element</div>
<script>

var text_content = document.getElementById("content").innerText;
function Add(add, before, in_content)
    {
        var p = in_content.indexOf(before);
        if (p !== -1)
            return [in_content.slice(0, p), add, in_content.slice(p-1)].join('');
        else
            return false;

    }

    var result = Add("paragraph","this",text_content);
    if(result !== false)
        document.getElementById("content").innerText = result;
</script>  
MozzieMD
  • 355
  • 2
  • 12
  • If you need to get it from the dom parse and put back i can create this function in javascript + jquery! – MozzieMD Aug 25 '15 at 11:04
  • There is Javascript, hope it helped! If you need to add the string after some text, tell me and i will change, cause i didn't understand, before or after! – MozzieMD Aug 25 '15 at 11:28