insert_with_markers

Definition:
function insert_with_markers( $filename, $marker, $insertion ) {}

Inserts an array of strings into a file (.htaccess ), placing it between BEGIN and END markers. Replaces existing marked info. Retains surrounding data. Creates file if none exists.

Parameters

  • unknown_type $filename
  • unknown_type $marker
  • unknown_type $insertion

Return values

returns:True on write success, false on failure.

Source code

function insert_with_markers( $filename, $marker, $insertion ) {

	if (!file_exists( $filename ) || is_writeable( $filename ) ) {

		if (!file_exists( $filename ) ) {

			$markerdata = '';

		} else {

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

		}



		if ( !$f = @fopen( $filename, 'w' ) )

			return false;



		$foundit = false;

		if ( $markerdata ) {

			$state = true;

			foreach ( $markerdata as $n => $markerline ) {

				if (strpos($markerline, '# BEGIN ' . $marker) !== false)

					$state = false;

				if ( $state ) {

					if ( $n + 1 < count( $markerdata ) )

						fwrite( $f, "{$markerline}\n" );

					else

						fwrite( $f, "{$markerline}" );

				}

				if (strpos($markerline, '# END ' . $marker) !== false) {

					fwrite( $f, "# BEGIN {$marker}\n" );

					if ( is_array( $insertion ))

						foreach ( $insertion as $insertline )

							fwrite( $f, "{$insertline}\n" );

					fwrite( $f, "# END {$marker}\n" );

2021

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: