Skip to content

Instantly share code, notes, and snippets.

@notnian
Created March 25, 2020 09:27
Show Gist options
  • Save notnian/ef74bc973502e104fb020b1a5f5e1710 to your computer and use it in GitHub Desktop.
Save notnian/ef74bc973502e104fb020b1a5f5e1710 to your computer and use it in GitHub Desktop.
substr — Retourne un segment de chaîne
<?php
// CODE FROM https://www.php.net/manual/fr/function.substr.php
// biohazard dot ge at gmail dot com
// may be by following functions will be easier to extract the needed sub parts from a string:
after ('@', 'biohazard@online.ge');
//returns 'online.ge'
//from the first occurrence of '@'
before ('@', 'biohazard@online.ge');
//returns 'biohazard'
//from the first occurrence of '@'
between ('@', '.', 'biohazard@online.ge');
//returns 'online'
//from the first occurrence of '@'
after_last ('[', 'sin[90]*cos[180]');
//returns '180]'
//from the last occurrence of '['
before_last ('[', 'sin[90]*cos[180]');
//returns 'sin[90]*cos['
//from the last occurrence of '['
between_last ('[', ']', 'sin[90]*cos[180]');
//returns '180'
//from the last occurrence of '['
// here comes the source:
function after ($this, $inthat)
{
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
};
function after_last ($this, $inthat)
{
if (!is_bool(strrevpos($inthat, $this)))
return substr($inthat, strrevpos($inthat, $this)+strlen($this));
};
function before ($this, $inthat)
{
return substr($inthat, 0, strpos($inthat, $this));
};
function before_last ($this, $inthat)
{
return substr($inthat, 0, strrevpos($inthat, $this));
};
function between ($this, $that, $inthat)
{
return before ($that, after($this, $inthat));
};
function between_last ($this, $that, $inthat)
{
return after_last($this, before_last($that, $inthat));
};
// use strrevpos function in case your php version does not include it
function strrevpos($instr, $needle)
{
$rev_pos = strpos (strrev($instr), strrev($needle));
if ($rev_pos===false) return false;
else return strlen($instr) - $rev_pos - strlen($needle);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment