PHP snippets

Strip multiple slashes to leave a single slash

File: /admin/htaccess.php

$string = preg_replace('#/{2,}#', '/', $string);

preg_replace means replace "this", "with this", "in this"

/{2,} = 2 or more of /

# = delimiters.

A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. Often used delimiters are forward slashes (/), hash signs (#) and tildes (~)

Also works:

$string = preg_replace('#/+#', '/', $string);

The pattern /+ will match the forward slash / one or more times, and will replace it with a single /

Using a loop:

while(strpos($string, '//') !== FALSE ) {
   $string = str_replace('//', '/', $string);
}

This will call str_replace until all occurrences of // are replaced.


'Get between' function

File: /admin/htaccess.php

function getBetween($content, $start, $end) {
  $r = explode($start, $content);
  if (isset($r[1])) {
    $r = explode($end, $r[1]);
    return $r[0];
  }
  return '';
}

// Deployment
$to_replace = getBetween($target, '# START marker', '# END marker');

The target string contains 'marker' strings placed at the start and end of something to be replaced. When the function is deployed, the 'to_replace' string replaces whatever is between the two markers.

The explode() function breaks a string into an array $r. The first variables ($start and $end) are 'separators' that specify the points at which the string will split, i.e. whenever this string is found in the target string it symbolizes the end of one element of the array and start of another.

$r[0] = the first element of the array.

$r[1] = the second element of the array.

The $content variable exists only inside the function. When the function is deployed it becomes the 'target' string.

The function splits the target twice.

To begin with, $r = explode($start, $content); makes the content into an array in which the separator is $start. $r[1] is the second element of the array, i.e. everything after $start. That second element is then exploded, this time using $end as the separator. $r[0] is the first element of the new array, i.e. everything before $end. The function then 'returns' it as the part of the target string to be replaced.

Deploying the function substitutes $start with '# START marker' and $end with '# END marker'.


Give images an absolute path

File: /inc/functions.php

<img src="img/spaceman.jpg" to:

<img src="https://domain.com/img/spaceman.jpg"

function img_path($content) {
  $content = preg_replace('!(<img src=")(img/|/img/)([A-Za-z0-9_-]+.)(jpg|jpeg|gif|png)"!', '$1' . LOCATION . 'img/$3$4"', $content);
  return $content;
}

// Deployment
$content = img_path($content);

preg_replace means replace "this", "with this", "in this"

// "this",
'!(<img src=")(img/|/img/)([A-Za-z0-9_-]+.)(jpg|jpeg|gif|png)"!'

Consists of four (groups) between exclamation mark 'delimiters':

!($1)($2)($3)($4)!

$1 (<img src=") = literal text

$2 (img/|/img/) matches img/ or /img/ (to match the image folder name)

$3 ([A-Za-z0-9_-]+.) matches one or more alphanumeric characters followed by one dot (to match image filename)

$4 (jpg|jpeg|gif|png) matches any of those file extensions

The ensemble matches (<img src=")(img/)(spaceman.)(jpg)"

// "with this",
'$1' . LOCATION . 'img/$3$4"'
// LOCATION = (php constant) = https://domain.com/

Assembles (<img src=")(https://domain.com/)img/(spaceman.)(jpg)

// "in this"
$content

$content is the target text.

Technical »

—   
Page last modified: 13 April, 2024
Search | Legal | Qwwwik
Patrick Taylor

Menu