PHP Parse Options

by admin on January 26, 2012 · 0 comments

PHP Parse Options



Instructions:

The parse_url Function

PHPs "parse_url" function lets you input a URL and have it broken down into an array that contains its various components. It checks for, and returns the scheme, host, port, user, pass, path, query and fragment, as long as those pieces exist — not all URLs contain each of these components. You can use partial or whole URLs with this function; however, it alone does not validate that the given URL is complete or safe. You can also include an optional second parameter to the parse_url function to indicate you want it to return a string and not an array.

The date_parse Function

Similar to PHP’s URL-parsing function, the "date_parse" function lets you input a date and break it down into its component pieces, each saved as a different element in an array. You can pass a full or partial date; the function will return the components it finds, including the year, month, day, hour, minute, second, fraction, warning_count, warnings, error_count, errors, and whether the date is a local time. The warnings and errors components are both arrays themselves.

The parse_str Function

PHPs "parse_str" function lets you input a query string that you want to convert to variables. For example, passing "varA=one&varB=two" will create two variables, "varA" and "varB," with the values "one" and "two," respectively. The function itself does not return a value by default, but you can choose to save the values to an associative array instead of variables. You will usually use this function as part of a larger process — for example, such as using the "parse_url" function to extract the query component from a URL, then passing that value to the "parse_str" to save the query to variables.

The preg_match Function

PHPs "preg_match" function lets you create a list of tokens, called a regular expression, and match those tokens against a string. At a minimum, you must provide a pattern and a string. PHP parses the string with the pattern. You can create a pattern, such as a substring of text or a punctuation character. You can also specify a variable to save the results of the parse into, flags and offset values.

Previous post:

Next post: