1

我只想知道是否有任何内置的 php 函数,我可以在给定的两个关键字(keyword1 和 keyword2)之间获取子字符串。请注意,关键字可能在字符串中重复,因此我必须能够获得第 x 个关键字 1 和第 y 个关键字 2 之间的子字符串。此外,我主要使用 unicode 字符,因此该函数应该是独立于字符集的。

请帮我解决这个问题。

例如 $string=这是他们在剧院里戴着帽子的猫。$keyword1="是"; $keyword2="的";

任务:如何在上面给定的字符串中获取第二次出现的“is”和第三次出现的“the”之间的子字符串。

答案:“戴帽子的猫”

4

2 回答 2

1

You can use regular expressions:

$string = "This is their cat with a hat in the theater";

$regex1 = "/.*? is |^is/";
$regex2 = "/ the .*| the$/";
echo preg_replace($regex1, '', preg_replace($regex2, ' the', $string));

EDIT Here is more generic code:

function find($text, $str, $offset) {
    $len = strlen($text);
    $search_len = strlen($str);
    $count = 0;
    for ($i=0; $i<$len; ++$i) {
        if (substr($text, $i, $search_len) == $str) {
            if (++$count == $offset) {
                return $i;
            }
        }
    }
    return -1;
}

function between($text, $word1, $offset1, $word2, $offset2) {
    $start = find($text, $word1, $offset1);
    $end = find($text, $word2, $offset2);
    if ($start != -1 && $end != -1) {
        return substr($text, $start + strlen($word1), $end-$start-strlen($word2));
    } else {
        return '';
    }
}


$string = "This is their cat with a hat in the theater";

echo between($string, 'is', 2, 'the', 3);
echo between($string, 'at', 1, 'at', 3);
于 2013-07-11T13:26:20.590 回答
0

以下两个函数的组合适用于任何字符串,包括 unicode 字符:

//Gets the position of a given substring with its offset;
function strposOffset($string, $search, $offset)
{
    /*** explode the string ***/
    $arr = explode($search, $string);
    /*** check the search is not out of bounds ***/
   switch( $offset )
   {
       case $offset == 0:
         return false;
         break;

       case $offset > max(array_keys($arr)):
         return false;
         break;

       default:
         return mb_strlen(implode($search, array_slice($arr, 0, $offset)), "utf-8");
   }
} //Source: www.phpro.org



//Extracts a substring between given two given substrings with their offsets.
function extractMiddleSubstr($string, $substr1, $offset1, $substr2, $offset2){

   $strlen_substr1 = mb_strlen($substr1, "utf-8"); //length of substr1;
   $strpos_substr1  = strposOffset($string, $substr1, $offset1); //position of substr1;
   $strpos_substr2  = strposOffset($string, $substr2, $offset2); //position of substr2;

    if($strpos_substr1!==null && $strpos_substr2!==null && $strpos_substr1!==false && $strpos_substr2!==false){

       if($strpos_substr1<=$strpos_substr2){

          $strpos_substr    = $strlen_substr1+$strpos_substr1; //position of substr;
          $strlen_substr  = $strpos_substr2-$strpos_substr;  //length of substr;

          $substr       = mb_substr($string, $strpos_substr, $strlen_substr, "utf-8"); //substr;
          $substr           = trim($substr); // removes whitespaces;

          return $substr;
        }else{
          return false;
        }

    }else{
        return false;
    }
}
于 2013-07-16T07:37:30.823 回答