note 42412 modified in function.substr by danbrown

From: Date: Fri, 28 Nov 2008 20:26:51 +0000
Subject: note 42412 modified in function.substr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147213@lists.php.net to get a copy of this message
may be by following functions will be easyer 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 '[' <?php 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)); }; // USES 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); }; ?> --was-- may be by following functions will be easyer 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 '[' <? 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)); }; // USES 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); }; ?> http://php.net/manual/en/function.substr.php

« previous php.notes (#147213) next »