extract_from_markers( string $filename, string $marker ): string[]

In this article

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

Parameters

$filenamestringrequired
Filename to extract the strings from.
$markerstringrequired
The marker to extract the strings from.

Return

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

Source

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;
}

Changelog

VersionDescription
1.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.