Method to extract key/value pairs out of a string with xml style attributes

@param   string  $string String containing xml style attributes
@return  array   Key/Value pairs for the attributes
772179
public function parseAttributes($string)
    {
        $result = array();

        if (!empty($string))
        {
            $pattern = '#(?(DEFINE)
                (?<name>[a-zA-Z][a-zA-Z0-9-_:]*)
                (?<value_double>"[^"]+")
                (?<value_none>[^\s>]+)
                (?<value>((?&value_double)|(?&value_none)))
            )
            (?<n>(?&name))[\s]*(=[\s]*(?<v>(?&value)))?#xs';

            if (preg_match_all($pattern, $string, $matches, PREG_SET_ORDER))
            {
                foreach ($matches as $match) {
                    if (!empty($match['n'])) {
                        $result[$match['n']] = isset($match['v']) ? trim($match['v'], '\'"') : '';
                    }
                }
            }
        }

        return $result;
    }
627632
protected function clean($string)
    {
        if ($this->format == 'xml') {
            return $string;
        }

        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
        $string = htmlentities($string, ENT_QUOTES, 'UTF-8');

        if (function_exists('mb_convert_encoding')) {
            $string = mb_convert_encoding($string, 'UTF-8');
        } else {
            $string = utf8_decode($string);
        }

        return $string;
    }
560764
public static function fromHeader(string $string): array
    {
        $cookies = \explode(";", $string);
        $result = [];

        try {
            foreach ($cookies as $cookie) {
                $parts = \explode('=', $cookie, 2);

                if (2 !== \count($parts)) {
                    return [];
                }

                list($name, $value) = $parts;

                // We can safely trim quotes, as they're not allowed within cookie values
                $result[] = new self(\trim($name), \trim($value, " \t\""));
            }
        } catch (InvalidCookieException $e) {
            return [];
        }

        return $result;
    }
1352689
public static function getTagData( $string, $transformKeys = 0 )
	{
		$string		= trim( $string );
		$attributes	= array();
		$content	= "";
		$nodename	= "";

		if( preg_match( "@^<([a-z]+)@", $string, $results ) )
			$nodename	= $results[1];
		if( preg_match( "@>([^<]*)<@", $string, $results ) )
			$content	= $results[1];
		if( preg_match_all( '@ (\S+)="([^"]+)"@', $string, $results ) )
		{
			$array	= array_combine( $results[1], $results[2] );
			foreach( $array as $key => $value )
			{
				if( $transformKeys == self::TRANSFORM_LOWERCASE )
					$key	= strtolower( $key );
				else if( $transformKeys == self::TRANSFORM_UPPERCASE )
					$key	= strtoupper( $key );
				$attributes[$key]	= $value;
			}
		}
		if( preg_match_all( "@ (\S+)='([^']+)'@", $string, $results ) )
		{
			$array	= array_combine( $results[1], $results[2] );
			foreach( $array as $key => $value )
			{
				if( $transformKeys == self::TRANSFORM_LOWERCASE )
					$key	= strtolower( $key );
				else if( $transformKeys == self::TRANSFORM_UPPERCASE )
					$key	= strtoupper( $key );
				$attributes[$key]	= $value;
			}
		}
		return array(
			'nodename'		=> $nodename,
			'content'		=> $content,
			'attributes'	=> $attributes
		);
	}
817028
private function parseStringToArray($string)
    {
        // convert commas to spaces
        $string = str_replace(',', ' ', $string);
        // convert new lines to spaces
        $string = str_replace(PHP_EOL, ' ', $string);
        // split on the spaces and filter out empty elements
        return array_values(
            array_filter(
                array_map(
                    'trim',
                    explode(' ', $string)
                ),
                'strlen'
            )
        );
    }
282435
protected function parseDocument( $string )
    {
        $document = array();

        // Read document headers
        while ( preg_match( '(\\A(?P<header>[A-Za-z0-9-]+):\s+(?P<value>.*)$)Sm', $string, $match ) )
        {
            $document[$match['header']] = trim( $match['value'] );
            $string = substr( $string, strlen( $match[0] ) + 1 );
        }

        if ( strpos( $document['Content-Type'], 'multipart/mixed' ) === 0 )
        {
            // Rebuild full document
            $body = fopen( 'string://', 'w' );
            foreach ( $document as $key => $value )
            {
                fwrite( $body, "$key: $value\r\n" );
            }
            fwrite( $body, $string );
            fseek( $body, 0 );

            $document['body'] = array();
            $parser = new phpillowToolMultipartParser( $body );
            while ( ( $part = $parser->getDocument() ) !== false )
            {
                $document['body'][] = $part;
            }
        }
        else
        {
            $document['body'] = trim( $string );
        }

        return $document;
    }
821428
private function getStyleStack($string)
    {
        if (preg_match_all(static::REGEX_FORMAT, $string, $matches)) {
            foreach ($matches[1] as $grouping) {
                if (preg_match_all(static::REGEX_STYLE_ITEM, $grouping, $styles)) {
                    foreach ($styles[0] as $style) {
                        yield $style;
                    }
                } else {
                    yield static::STYLE_RESET;
                }
            }
        }
        return;
    }
787723
private function parseQuery($string)
    {
        $return = array();
        $parts = explode("&", $string);
        foreach ($parts as $part) {
            list($key, $value) = explode('=', $part, 2);
            $value = urldecode($value);
            if (isset($return[$key])) {
                if (!is_array($return[$key])) {
                    $return[$key] = array($return[$key]);
                }
                $return[$key][] = $value;
                continue;
            }
            $return[$key] = $value;
        }
        return $return;
    }
93983
public static function parseAttributes($string)
	{
		// Initialise variables.
		$attr = array();
		$ret  = array();

		// Let's grab all the key/value pairs using a regular expression
		preg_match_all('/([\w:-]+)[\s]?=[\s]?"([^"]*)"/i', $string, $attr);

		if (is_array($attr))
		{
			$numPairs = count($attr[1]);
			for ($i = 0; $i < $numPairs; $i++)
			{
				$ret[$attr[1][$i]] = $attr[2][$i];
			}
		}

		return $ret;
	}
1218908
protected function xmlentities($string) { 
   		return str_replace( array( '&', '"', "'", '<', '>', '’' ), array( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;', '&apos;' ), $string);
	}
1238410
public static function stringToValueAndUnit(string $string): array
    {
        preg_match('/^([0-9.]+)([a-zA-Z]+)$/', $string, $matches);
        $value = (float) $matches[1];
        $unit = Mapper::getUnitFromAbbreviation($matches[2]);
        return [$value, $unit];
    }
593071
public static function stringToArray(string $string): array
    {
        list($from, $to) = explode(self::SEPARATOR, $string);
        $from = empty($from)
            ? self::ZERO
            : (is_numeric($from)
                ? (int) $from
                : $from);

        $to = empty($to)
            ? self::INFINITE
            : (is_numeric($to)
                ? (int) $to
                : $to);

        return [$from, $to];
    }
1015583
public function transform($string)
    {
        $xml = simplexml_load_string($string);

        if (false === $xml) {
            throw new \Exception('Unable to parse xml string.');
        }

        return  $this->xmlToArray($xml)['feed'];
    }
408887
public function parseStyle($string) {
        $groups = [];

        if (preg_match_all("#([a-z]+)=([a-z,]+)#", $string, $matches, PREG_SET_ORDER) !== false) {
            $groups = $matches;
        }

        return $groups;
    }
1335629
protected function getStringQueryParameters($string)
    {
        $query = parse_url($string, PHP_URL_QUERY);

        $params = [];
        if ($query) parse_str($query, $params);

        return $params;
    }
169980
public function parseString($string)
    {
        $tokens = $this->tokenize($string);

        $tokens = array_filter(
            $tokens,
            function ($token) {
                return !($token['type'] === TransformationProcessor::T_WHITESPACE ||
                          $token['type'] === TransformationProcessor::T_COMMENT);
            }
        );

        $ast = array();
        $section = null;
        while ($token = array_shift($tokens)) {
            if ($token['type'] === TransformationProcessor::T_SECTION) {
                $section = $token['data']['section'];
                continue;
            }

            if ($section === null) {
                throw new RuntimeException('Expected section.');
            }

            $ast[$section][] = $token;
        }

        return $ast;
    }
873655
protected function parseString($string)
    {
        $array  = array();

        if (strlen($string) < 1) {
            return $array;
        }

        // Separate single string into an array of "key=value" strings
        $keyvalue = explode('&', $query_string);

        // Separate each "key=value" string into an array[key] = value
        foreach ($keyvalue as $pair) {
            list($k, $v) = explode('=', $pair, 2);

            // Handle the case where multiple values map to the same key
            // by pulling those values into an array themselves
            if (isset($query_array[$k])) {
                // If the existing value is a scalar, turn it into an array
                if (is_scalar($query_array[$k])) {
                    $query_array[$k] = array($query_array[$k]);
                }
                array_push($query_array[$k], $v);
            } else {
                $query_array[$k] = $v;
            }
        }

        return $array;
    }