extract_from_markers()WP 1.5.0

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

No Hooks.

Return

String[]. An array of strings from a file (.htaccess) from between BEGIN and END markers.

Usage

extract_from_markers( $filename, $marker );
$filename(string) (required)
Filename to extract the strings from.
$marker(string) (required)
The marker to extract the strings from.

Changelog

Since 1.5.0 Introduced.

extract_from_markers() code WP 6.6.2

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

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

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

	$state = false;

	foreach ( $markerdata as $markerline ) {
		if ( str_contains( $markerline, '# END ' . $marker ) ) {
			$state = false;
		}

		if ( $state ) {
			if ( str_starts_with( $markerline, '#' ) ) {
				continue;
			}

			$result[] = $markerline;
		}

		if ( str_contains( $markerline, '# BEGIN ' . $marker ) ) {
			$state = true;
		}
	}

	return $result;
}