php PHP函数删除字符串中某些字符之间的所有字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13031250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:40:53  来源:igfitidea点击:

PHP function to delete all between certain character(s) in string

phpstring

提问by Milo? ?akonovi?

I'm interested in function delete_all_between($char1, $char2, $string)that will search given $string for $char1 and $char2 and, if such has been found, clear $string from substring between these two characters, including$char1 and $char2 itself.

我感兴趣的function delete_all_between($char1, $char2, $string)是将在给定的 $string 中搜索 $char1 和 $char2,如果找到了,从这两个字符之间的子字符串中清除 $string,包括$char1 和 $char2 本身。

Example:

例子:

$string = 'Some valid and <script>some invalid</script> text!';
delete_all_between('<script>', '</script>', $string);

Now, $string should contain just

现在, $string 应该只包含

'Some valid and  text'; //note two spaces between 'and  text'

Does someone have quick solution?

有人有快速解决方案吗?

回答by Tim S

<?php

$string = 'Some valid and <script>some invalid</script> text!';
$out = delete_all_between('<script>', '</script>', $string);
print($out);

function delete_all_between($beginning, $end, $string) {
  $beginningPos = strpos($string, $beginning);
  $endPos = strpos($string, $end);
  if ($beginningPos === false || $endPos === false) {
    return $string;
  }

  $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);

  return delete_all_between($beginning, $end, str_replace($textToDelete, '', $string)); // recursion to ensure all occurrences are replaced
}

回答by Stan Smulders

Here's a oneliner:

这是一个单线:

preg_replace('/START[\s\S]+?END/', '', $string);

preg_replace('/START[\s\S]+?END/', '', $string);

Replace STARTand END:) Credits go to another SO thread!

替换STARTEND:) 积分转到另一个 SO 线程!

回答by Jevgenij Volosatov

I think substr()works too slow. The best way is:

我认为substr()工作太慢了。最好的办法是:

return substr($string, 0, $beginningPos) . 
       substr($string, $endPos + strlen($end));

回答by Alexey Abraham

Actually, I was looking for a function, which gives me simple and stable solution to grab out all the variables of TWIG template. The proposed regexps did not work well for many reasons so I decided to go by just erasing all the content between tags instead of counting tags ^_^.

实际上,我正在寻找一个函数,它为我提供了简单而稳定的解决方案来抓取 TWIG 模板的所有变量。由于多种原因,建议的正则表达式不能很好地工作,所以我决定只擦除标签之间的所有内容,而不是计算标签 ^_^。

/**
     * deletes ALL the string contents between all the designated characters
     * @param $start - pattern start 
     * @param $end   - pattern end
     * @param $string - input string, 
     * @return mixed - string
     */
    function auxDeleteAllBetween($start, $end, $string) {
        // it helps to assembte comma dilimited strings
        $string = strtr($start. $string . $end, array($start => ','.$start, $end => chr(2)));
        $startPos  = 0;
        $endPos = strlen($string);
        while( $startPos !== false && $endPos !== false){
            $startPos = strpos($string, $start);
            $endPos = strpos($string, $end);
            if ($startPos === false || $endPos === false) {
                $run = false;
                return $string;
            }
            $textToDelete = substr($string, $startPos, ($endPos + strlen($end)) - $startPos);
            $string = str_replace($textToDelete, '', $string);
        }
        return $string;
    }

    /**
     * This function is intended to replace
     * //preg_match_all('/\{\%\s*([^\%\}]*)\s*\%\}|\{\{\s*([^\}\}]*)\s*\}\}/i', $this->_tplSubj, $matchesSubj);
     * which did not give intended results for some reason.
     *
     * @param $inputTpl
     * @return array
     */
    private function auxGetAllTags($inputTpl){
        $inputTpl = strtr($inputTpl, array('}}' => ','.chr(1), '{{' => chr(2)));
        return explode(',',$this->auxDeleteAllBetween(chr(1),chr(2),$inputTpl));
    }


$template = '<style>
td{border-bottom:1px solid #eee;}</style>
<p>Dear {{jedi}},<br>New {{padawan}} is waiting for your approval: </p>
<table border="0">
<tbody><tr><td><strong>Register as</strong></td><td>{{register_as}}, user-{{level}}</td></tr>
<tr><td><strong>Name</strong></td><td>{{first_name}} {{last_name}}</td></tr>...';

print_r($this->auxGetAllTags($template));

回答by lopamudra

You can use double str_replace() $q = str_replace('<script>', '', $string); $p = str_replace('some invalid', '', $q); echo $p;

您可以使用双 str_replace() $q = str_replace('<script>', '', $string); $p = str_replace('some invalid', '', $q); echo $p;