Common start string

Find and return the longest common start string of two strings.

@param string $string1
@param string $string2
@return string
482059
protected function commonStartString($string1, $string2)
    {
        $length = min(strlen($string1), strlen($string2));
        $common = '';
        for ($i = 0; $i < $length && $string1[$i] === $string2[$i]; ++$i) {
            $common .= $string1[$i];
        }

        return $common;
    }
318808
public static int commonOverlap(String text1, String text2) {
        // Cache the text lengths to prevent multiple calls.
        int text1_length = text1.length();
        int text2_length = text2.length();
        // Eliminate the null case.
        if (text1_length == 0 || text2_length == 0) {
            return 0;
        }
        // Truncate the longer string.
        if (text1_length > text2_length) {
            text1 = text1.substring(text1_length - text2_length);
        } else if (text1_length < text2_length) {
            text2 = text2.substring(0, text1_length);
        }
        int text_length = Math.min(text1_length, text2_length);
        // Quick check for the worst case.
        if (text1.equals(text2)) {
            return text_length;
        }

        // Start by looking for a single character match
        // and increase length until no match is found.
        // Performance analysis: http://neil.fraser.name/news/2010/11/04/
        int best = 0;
        int length = 1;
        while (true) {
            String pattern = text1.substring(text_length - length);
            int found = text2.indexOf(pattern);
            if (found == -1) {
                return best;
            }
            length += found;
            if (found == 0 || text1.substring(text_length - length).equals(
                    text2.substring(0, length))) {
                best = length;
                length++;
            }
        }
    }
496886
function(str, start) {
    return (str.length >= start.length &&
            str.substr(0, start.length) === start);
  }
33589
public static function substr(string $string, int $start, int $length = null)
    {
        if ($length === 0) {
            return '';
        }

        // Cannot omit $length, when specifying charset
        if ($length === null) {
            // save internal encoding
            $enc = mb_internal_encoding();
            mb_internal_encoding('UTF-8');
            $str = mb_substr($string, $start);
            // restore internal encoding
            mb_internal_encoding($enc);

            return $str;
        }

        return mb_substr($string, $start, $length, 'UTF-8');
    }
301501
public static function substr_replace($string, $replacement, $start , $length=null )
    {
        $ret = '';
        if($start>0) $ret .= self::substr($string, 0, $start);
        $ret .= $replacement;
        if($length!=null) $ret .= self::substr($string, $start+$length);
        return $ret;
    }
929084
public function join($str1 = null, $str2 = null)
    {
        if (is_null($str1)) {
            $str1 = $this->number();
        }
        if (is_null($str2)) {
            $str2 = $this->vn();
        }

        return $str1 . $this->vnSeparator . $str2;
    }
1054896
public static function substring($string, $start = 0, $length = null)
    {
        if (!self::isValid($string)) {
            return false;
        }

        return mb_substr($string, $start, $length);
    }
718709
private function find_string($string, $start = -1, $end = -1) {
        if (($start == -1) or ($end == -1)) {
            // find_string is called with only one parameter, set start end end
            $start = 0;
            $end = $this->total;
        }
        if (abs($start - $end) <= 1) {
            // We're done, now we either found the string, or it doesn't exist
            $txt = $this->get_original_string($start);
            if ($string == $txt)
                return $start;
            else
                return -1;
        }
        else if ($start > $end) {
            // start > end -> turn around and start over
            return $this->find_string($string, $end, $start);
        }
        else {
            // Divide table in two parts
            $half = (int)(($start + $end) / 2);
            $cmp = strcmp($string, $this->get_original_string($half));
            if ($cmp == 0)
                // string is exactly in the middle => return it
                return $half;
            else if ($cmp < 0)
                // The string is in the upper half
                return $this->find_string($string, $start, $half);
            else
                // The string is in the lower half
                return $this->find_string($string, $half, $end);
        }
    }
52253
public function compareStart($string)
    {
        $lowerString = strtolower($string);
        foreach ($this->tokens as $token) {
            if (strpos($lowerString, strtolower($token)) === 0) {
                $this->found = true;
            }
        }
    }
802740
public static function startsWith($string, $substrings)
    {
        if (is_array($substrings)) {
            foreach ((array) $substrings as $substring) {
                if ($substring != '' && mb_strpos($string, $substring) === 0) {
                    return true;
                }
            }
        } elseif (is_string($substrings)) {
            if ($substrings != '' && mb_strpos($string, $substrings) === 0) {
                return true;
            }
        }

        return false;
    }
1183156
public static function startsWith($string, $prefix, $ignoreCase = false)
    {
        if ($ignoreCase) {
            $string = strtolower($string);
            $prefix = strtolower($prefix);
        }

        return $prefix == substr($string, 0, strlen($prefix));
    }
1363400
public function startWith( $prefix, $string, $unique = true )
	{
		return $unique && $this->startsWith( $prefix, $string )
			? $string
			: $prefix . $string;
	}
1112535
private void start(String str1, String str2) {
        result = (Result) results.get(str1 + str2);
        if (result == null) {
            result = new Result();
            result.object = str1;
            result.name = str2;
            results.put(str1 + str2, result);
            resultList.add(result);
        }
        start = System.currentTimeMillis();
    }
382904
protected function getStringsBetween($string, $start, $end)
    {

        $pattern = sprintf(
                '/%s(.*?)%s/', preg_quote($start), preg_quote($end)
        );

        preg_match_all($pattern, $string, $matches);

        return $matches[1];
    }
353949
public function startsWith(string $string) : bool
    {
        return substr($this->path, 0, strlen($string)) === $string;
    }
354830
public static function byteStrBetween($string, $start, $end = null)
    {
        if (($startPos = static::byteStrPos($string, $start)) !== false) {
            if ($end) {
                if (($end_pos = static::byteStrPos($string, $end, $startPos + static::byteLength($start))) !== false) {
                    return static::byteSubstr($string, $startPos + static::byteLength($start), $end_pos - ($startPos + static::byteLength($start)));
                }
            } else {
                return static::byteSubstr($string, $startPos);
            }
        }
        return false;
    }
1265848
public static function truncate2(string $str, int $start, int $length = null): string
    {
        if (!$length) {
            $length = $start;
            $start  = 0;
        }

        if (\strlen($str) <= $length) {
            return $str;
        }

        if (\function_exists('mb_substr')) {
            $str = \mb_substr(\strip_tags($str), $start, $length, 'utf-8');
        } else {
            $str = \substr($str, $start, $length) . '...';
        }

        return $str;
    }