php - 替换忽略 HTML 标签的文本

标签 php regex preg-replace

我有一个带有 HTML 标签的简单文本,例如:

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.

当我执行此替换时,我需要替换此文本的某些部分而忽略其 html 标记,例如此字符串 - Thus, <i>only</i> while我需要替换为我的字符串 Hello, <i>its only</i> while .要替换的文本和字符串是动态的。我的 preg_replace 模式需要你的帮助

$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';

请帮助我找到解决方案。谢谢

最佳答案

基本上,我们将使用 Regex 从纯文本构建匹配项和模式的动态数组。此代码仅匹配最初要求的内容,但您应该能够从我拼写的方式中了解如何编辑代码。我们捕获打开或关闭标记和空白作为传递变量,并替换它周围的文本。这是基于两个和三个单词组合的设置。

<?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;


?>

输出

<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

关于php - 替换忽略 HTML 标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9404527/

相关文章:

PHP表单不插入数据

javascript - 如何在 NodeJS 中控制应用程序的流程

javascript - 如何在JS中从外部url解码多维JSON?

regex - 如何在redis keys命令中应用 'OR'条件

.NET Regex 重叠匹配数

php - 用于匹配(并删除)包含文件中的多行 PHP 代码的正则表达式模式

php - 在没有超链接的情况下用超链接替换文本

PHP Mysql更新集替换完全匹配

php - CakePHP 2.0 基本身份验证始终提供 302 重定向而不是 401 未授权

c# - 在 C# 中计算与 Regex 的重叠匹配