VersionPress\Utils\StringUtils::replaceFirst PHP Method

replaceFirst() public static method

Replaces the first occurence.
public static replaceFirst ( string $needle, string $replace, string $haystack ) : string
$needle string
$replace string
$haystack string
return string
    public static function replaceFirst($needle, $replace, $haystack)
    {
        $needlePosition = strpos($haystack, $needle);
        if ($needlePosition === false) {
            return $haystack;
        }
        return substr_replace($haystack, $replace, $needlePosition, strlen($needle));
    }

Usage Example

 /**
  * Converts PHP-serialized string to mutiple INI lines.
  *
  * For example string 'a:1:{i:0;s:3:"foo";}' with key 'data' is converted to
  * [
  *  'data = <<<serialized>>> <array>',
  *  'data[0] = "foo"'
  * ]
  *
  * See IniSerializerTest for more examples.
  *
  * @param string $key
  * @param string $serializedData
  * @return string[]
  */
 public static function toIniLines($key, $serializedData)
 {
     self::$value = $serializedData;
     $parsingResult = self::parseSerializedString();
     $iniLines = self::convertParsingResultToIni($key, $parsingResult);
     self::$index = 0;
     self::$value = null;
     // Add marker
     $iniLines[0] = StringUtils::replaceFirst(' = ', " = " . self::SERIALIZED_MARKER . " ", $iniLines[0]);
     return $iniLines;
 }