Php 查找具有两个重叠字符串的字符串

Php 查找具有两个重叠字符串的字符串,php,Php,我有两个字符串abc和bca。我想检查另一个字符串是否包含这两个字符串并且没有重叠。例如: 字符串abca包含这两个元素,但重叠 字符串abcxbca既包含这两个字符串,又不重叠 字符串abcxbcabc包含这两个元素,但重叠 享受:) 到目前为止您尝试了什么?请在下面添加您的代码 <?php $y1="abc"; //search string1 $y2="bca"; //search string2 $x1="abca"; //contains b

我有两个字符串
abc
bca
。我想检查另一个字符串是否包含这两个字符串并且没有重叠。例如:

  • 字符串abca包含这两个元素,但重叠
  • 字符串abcxbca既包含这两个字符串,又不重叠
  • 字符串abcxbcabc包含这两个元素,但重叠
  • 享受:)


    到目前为止您尝试了什么?请在下面添加您的代码
        <?php 
    
        $y1="abc"; //search string1
        $y2="bca"; //search string2
    
        $x1="abca"; //contains both but is overlapped.
        $x2= "abcxbca"; //contains both and not overlapped.
        $x3 ="abcxbcabc"; //contains both but is overlapped.
    
        class Points{
        public $start;
        public $stop;
        public $overlaped=false;
    
        private function Points( $s1, $s2 ) {
            $this->start=$s1;
            $this->stop=$s2;
            $this->overlaped=false;
        }
    
    
        public static function create($s1, $s2){
            if (is_numeric($s1)&&is_numeric($s2)){
                return new Points($s1, $s2);
            }
        else return NULL;
        }
    
    
    }  
    
    function display($arr){
        $cnt1=0;
        $cnt2=0;
        for ($i=0;$i<count($arr);$i++){
            if ($arr[$i]->overlaped===true) $cnt1++;
            else $cnt2++;
        }
    
        return " ".$cnt2." not overlaped and ".$cnt1." overlaped";
    }
    
    function strpos_all($haystack, $needle) {  
        $offset = 0;
        $allpos = array();
    
        while (($pos = strpos($haystack, $needle, $offset)) !== FALSE) {
            $offset   = $pos + 1;
            $allpos[] = Points::create($pos,$pos+strlen($needle));
        }
        return $allpos;   }
    
    
         function check($haystack,$needle1,$needle2){
    
        $find1= strpos_all($haystack, $needle1);
    
        $find2= strpos_all($haystack, $needle2);
    
    
    
        for ($i=0;$i<count($find1);$i++){
            for($j =0; $j<count($find2);$j++){
                if (max($find1[$i]->start,$find2[$j]->start) <= min($find1[$i]->stop,$find2[$j]->stop)) {
                  $find1[$i]->overlaped=true;
                  $find2[$j]->overlaped=true;
                }
            }
        }
    
        echo  $needle1.display($find1)."<br>"; 
        echo  $needle2.display($find2)."<br>"; 
    
    }
    
    echo "<br>------Start-------<br>";
    echo "String :".$x1."<br>";
    check ($x1,$y1,$y2);
    echo "<br>-----End----------<br>";
    
    echo "<br>------Start-------<br>";
    echo "String :".$x2."<br>";
    check ($x2,$y1,$y2);
    echo "<br>-----End--------<br>";
    
    echo "<br>------Start-------<br>";
    echo "String :".$x3."<br>";
    check ($x3,$y1,$y2);
    echo "<br>-----End--------<br>";
    
    ?>