Php str_i替换为数组和排除项,并限制替换

Php str_i替换为数组和排除项,并限制替换,php,dom,laravel-5.1,str-replace,simple-html-dom,Php,Dom,Laravel 5.1,Str Replace,Simple Html Dom,在我的一个项目中,客户要求内容中的品牌名称必须在每次出现在内容中时后跟“注册”商标 我真的不喜欢这样,因为高架的“®”非常难看,法律上也没有必要。每页一次(在上面的某个地方)就足够了 我创建了一个函数来满足他们的愿望: function replaceWith($content) { // Array with replacements to use // (removes also ® already in the physical content) $replace

在我的一个项目中,客户要求内容中的品牌名称必须在每次出现在内容中时后跟“注册”商标

我真的不喜欢这样,因为高架的“®”非常难看,法律上也没有必要。每页一次(在上面的某个地方)就足够了

我创建了一个函数来满足他们的愿望:

function replaceWith($content)
{
    // Array with replacements to use
    // (removes also ® already in the physical content)

    $replaces => [
        '®'         => '',
        'brandname' => 'BRANDNAME<sup>&reg;</sup>'
    ],

    // Array with excludes (I do not want url's etc to change too)
    $excludes => ['a', 'img', 'href']

    list($search, $replace) = array_divide($replaces);

        // Using simplehtmldom to load the DOM
        $html = new \Htmldom();
        $html->load($content);

        if (!empty($html))
        {
            foreach ($html->find('text') as $element)
            {
                if (!in_array($element->parent()->tag, $excludes))
                {
                    $element->innertext = str_ireplace($search, $replace, $element->innertext);
                }
             }
         $result = (string)$html;
     }
    $html->clear();
    return $result;
}
函数替换为($content)
{
//包含要使用的替换项的数组
//(同时删除物理内容中已有的®文件)
$replaces=>[
'®'         => '',
'brandname'=>'brandname®;'
],
//带有排除项的数组(我不希望url的等也改变)
$excludes=>['a','img','href']
列表($search,$replace)=数组除以($replaces);
//使用simplehtmldom加载DOM
$html=new\Htmldom();
$html->load($content);
如果(!empty($html))
{
foreach($html->find('text')作为$element)
{
如果(!in_数组($element->parent()->tag,$excludes))
{
$element->innertext=str\u ireplace($search,$replace,$element->innertext);
}
}
$result=(字符串)$html;
}
$html->clear();
返回$result;
}

现在我想更改它,因为我只想更改第一个事件,而不是全部关闭。str_ireplace没有可用的限制。我找到了一些解决方法,但它们都使用字符串或整数作为值,而不是数组作为搜索和替换值。

在str\u ireplace函数中,将最后一个参数设为1。经历那些不起作用的事情$计数器包含发生的替换总数。然后可以使用preg_replace()。在str_ireplace函数中执行,最后一个参数为1。经历那些不起作用的事情$计数器包含发生的替换总数。然后可以使用preg_replace()。经过