Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换PHP中第一个出现的不在引号标记之间的字符串_Php_Regex_String_Replace_Preg Replace - Fatal编程技术网

替换PHP中第一个出现的不在引号标记之间的字符串

替换PHP中第一个出现的不在引号标记之间的字符串,php,regex,string,replace,preg-replace,Php,Regex,String,Replace,Preg Replace,我想替换vbulletin论坛帖子中第一个出现的字符串(“[TOREPLACE]”),但前提是该字符串没有被bbcode quote标记包围 我的代码: if($firstpos = strpos($post['pagetext'], '[TOREPLACE]') !== false){ //check for quote if($quote_start = strpos($post['pagetext'], '[QUOTE') !== false && $qu

我想替换vbulletin论坛帖子中第一个出现的字符串(“[TOREPLACE]”),但前提是该字符串没有被bbcode quote标记包围

我的代码:

if($firstpos = strpos($post['pagetext'], '[TOREPLACE]') !== false){

    //check for quote
    if($quote_start = strpos($post['pagetext'], '[QUOTE') !== false && $quote_end = strpos($post['pagetext'], '[/QUOTE]') !== false){
        //quote found
        //check if quote starts before egg
        if($quote_start < $firstpos && $firstpos < $quote_end){

            //don't replace, because the string that is to be replace is in between quotes
        }
    }

    $post['pagetext_html'] = preg_replace('~\[TOREPLACE\]~i', '<img src="/src.jpg" border="0" />', $post['pagetext_html'], 1);

}
if($firstpos=strpos($post['pagetext'],'[TOREPLACE]')!==false){
//查看报价单
如果($quote_start=strpos($post['pagetext'],'[quote')!==false&$quote_end=strpos($post['pagetext'],'[quote]')!==false){
//找到报价单
//检查报价是否在鸡蛋之前开始
如果($quote_start<$firstpos&$firstpos<$quote_end){
//不要替换,因为要替换的字符串位于引号之间
}
}
$post['pagetext\u html']=preg\u replace(“~\[TOREPLACE\]~i',”$post['pagetext\u html'],1);
}
我必须循环这个来找到第一个不在引号之间的字符串。 这个报价检查很繁琐,也不是很优雅。有谁知道有更好的算法来完成上面的工作吗

算法:“如果在post中使用引号-查找并替换不在引号标记之间的第一次出现。如果没有引号,则替换第一次出现。”


也许我可以将所有这些添加到pre_匹配中。不过,引号是可选的,我不知道怎么做。

这稍微优雅一点,不会在多段引用文本上中断:

// for clarity, START and END are used instead of the actual (un)quote pattern

$input = "test text START quoted text END more text START quoted again END.";
$parts = preg_split("/(START.+?END)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE);

// $parts is an array containing quoted parts at odd indexes
print_r($parts);
输出:

Array
(
    [0] => test text 
    [1] => START quoted text END
    [2] =>  more text 
    [3] => START quoted again END
    [4] => .
)
然后循环遍历
$parts
,只更改偶数索引。之后:

$output = implode($parts);

另一个选项是
preg_replace_callback
,用于捕获报价范围和(使用
备选方案)标记内容,然后根据传入的匹配数组在回调中进行区分。