Using WordPress ‘extract_from_markers()’ PHP function

The extract_from_markers() WordPress PHP function is used to extract strings situated between BEGIN and END markers in the .htaccess file.

Usage

To use the extract_from_markers() function, you simply need to provide two arguments: the filename and the marker. These will specify from which file and which marker the strings should be extracted.

extract_from_markers($filename, $marker);

For instance, if we want to extract strings from a .htaccess file between the ‘# BEGIN WordPress’ and ‘# END WordPress’ markers, we would write:

$strings = extract_from_markers('.htaccess', 'WordPress');
print_r($strings);

This will return an array of strings that are located between the specified markers in the .htaccess file.

Parameters

  • $filename (string) – Required. This is the filename from which the strings will be extracted.
  • $marker (string) – Required. This is the marker that defines the section from which the strings will be extracted.

More information

See WordPress Developer Resources: extract_from_markers()

Examples

Extract WordPress section from .htaccess

This code extracts the WordPress section from the .htaccess file.

$strings = extract_from_markers('.htaccess', 'WordPress');
print_r($strings);

Extract custom section from .htaccess

This code extracts a custom section, named ‘MyCustomSection’, from the .htaccess file.

$strings = extract_from_markers('.htaccess', 'MyCustomSection');
print_r($strings);

Extract ‘PHP’ section from php.ini

This code extracts the ‘PHP’ section from a php.ini file.

$strings = extract_from_markers('php.ini', 'PHP');
print_r($strings);

Extract ‘Module Settings’ section from httpd.conf

This code extracts the ‘Module Settings’ section from the Apache httpd.conf file.

$strings = extract_from_markers('httpd.conf', 'Module Settings');
print_r($strings);

Use the extracted strings

This code extracts the ‘WordPress’ section from .htaccess file and iterates through the extracted strings.

$strings = extract_from_markers('.htaccess', 'WordPress');
foreach ($strings as $string) {
    echo $string . "\n";
}

This will echo each string located between the ‘# BEGIN WordPress’ and ‘# END WordPress’ markers in the .htaccess file.