extract_from_markers( string $filename, string $marker, bool $is_regex = false )

Extracts strings from between the BEGIN and END markers in the .htaccess file.


Parameters

$filename

(string) (Required) The path to the file to process (e.g. .htaccess).

$marker

(string) (Required) The marker string to search for (in the form of # BEGIN $marker and # END $marker in the file).

$is_regex

(bool) (Optional) Set to 'true' if $marker is a regex. The regex delimiter is '/'; you must escape this character if it occurs in your marker pattern. Default 'false'.

Default value: false


Return

(array) An array of strings from a file, extracted from between BEGIN and END markers.


Source

File: wp-admin/includes/misc.php

function extract_from_markers( $filename, $marker, $is_regex = false ) {
	$result = array();

	if ( ! file_exists( $filename ) ) {
		return $result;
	}

	$start_marker = "# BEGIN {$marker}";
	$end_marker   = "# END {$marker}";

	if ( ! $is_regex ) {
		$start_marker = preg_quote( $start_marker, '/' );
		$end_marker   = preg_quote( $end_marker, '/' );
	}

	$start_marker = "/{$start_marker}/";
	$end_marker   = "/{$end_marker}/";

	$file_data = explode( "\n", implode( '', file( $filename ) ) );

	$inside_markers = false;
	foreach ( $file_data as $line ) {
		if ( preg_match( $end_marker, $line ) ) {
			$inside_markers = false;
		}
		if ( $inside_markers ) {
			$result[] = $line;
		}
		if ( preg_match( $start_marker, $line ) ) {
			$inside_markers = true;
		}
	}

	return $result;
}

Changelog

Changelog
Version Description
1.0.0 Added $is_regex parameter.
WP-1.5.0 Introduced.