Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace text ignoring HTML tags

I have a simple text with HTML tags, for example:

Once <u>the</u> activity <a href="#">reaches</a> the resumed state, you can freely add and remove fragments to the activity. Thus, <i>only</i> while the activity is in the resumed state can the <b>lifecycle</b> of a <hr/> fragment change independently.

I need to replace some parts of this text ignoring its html tags when I do this replace, for example this string - Thus, <i>only</i> while I need to replace with my string Hello, <i>its only</i> while . Text and strings to be replaced are dynamically. I need your help with my preg_replace pattern

$text = '<b>Some html</b> tags with <u>and</u> there are a lot of tags <i>in</i> this text';

$arrayKeys= array('Some html' => 'My html', 'and there' => 'is there', 'in this text' => 'in this code');

foreach ($arrayKeys as $key => $value)
    $text = preg_replace('...$key...', '...$value...', $text);

echo $text; // output should be: <b>My html</b> tags with <u>is</u> there are a lot of tags <i>in</i> this code';

Please help me to find solution. Thank you

like image 231
pleerock Avatar asked Jun 15 '26 10:06

pleerock


1 Answers

Basically we're going to build dynamic arrays of matches and patterns off of plain text using Regex. This code only matches what was originally asked for, but you should be able to get an idea of how to edit the code from the way I've spelled it all out. We're catching either an open or a close tag and white space as a passthru variable and replacing the text around it. This is setup based on two and three word combinations.

<?php

    $text = '<b>Some html</b> tags with <u>and</u> there are a lot of tags <i>in</i> this text';

    $arrayKeys= array(
    'Some html' => 'My html',
    'and there' => 'is there',
    'in this text' =>'in this code');


    function make_pattern($string){
        $patterns = array(
                      '!(\w+)!i',
                      '#^#',
                      '! !',
                      '#$#');
        $replacements = array(
                      "($1)",
                      '!',
                //This next line is where we capture the possible tag or
                //whitespace so we can ignore it and pass it through.
                      '(\s?<?/?[^>]*>?\s?)',
                      '!i');
        $new_string = preg_replace($patterns,$replacements,$string);
        return $new_string;
    }

    function make_replacement($replacement){
        $patterns = array(
                      '!^(\w+)(\s+)(\w+)(\s+)(\w+)$!',
                      '!^(\w+)(\s+)(\w+)$!');
        $replacements = array(
                       '$1\$2$3\$4$5',
                       '$1\$2$3');
        $new_replacement = preg_replace($patterns,$replacements,$replacement);
        return $new_replacement;
    }


    foreach ($arrayKeys as $key => $value){
        $new_Patterns[] = make_pattern($key);
        $new_Replacements[] = make_replacement($value);
    }

    //For debugging
    //print_r($new_Patterns);
    //print_r($new_Replacements);

    $new_text = preg_replace($new_Patterns,$new_Replacements,$text);

    echo $new_text."\n";
    echo $text;


?>

Output

<b>My html</b> tags with <u>is</u> there are a lot of tags <i>in</i> this code
<b>Some html</b> tags with <u>and</u> there are a lot of tags <i>in</i> this text
like image 196
AbsoluteƵERØ Avatar answered Jun 17 '26 23:06

AbsoluteƵERØ