note 112707 added to function.substr

From: Date: Mon, 15 Jul 2013 14:30:23 +0000
Subject: note 112707 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-195619@lists.php.net to get a copy of this message
may be by following functions will be easier to extract the needed sub parts from a string: <?php 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: <?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)); }; // 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); }; ?> ---- Server IP: 69.147.83.199 Probable Submitter: 217.11.164.7 ---- Manual Page -- http://php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/112707 Del: integrated -- https://master.php.net/note/delete/112707/integrated Del: useless -- https://master.php.net/note/delete/112707/useless Del: bad code -- https://master.php.net/note/delete/112707/bad+code Del: spam -- https://master.php.net/note/delete/112707/spam Del: non-english -- https://master.php.net/note/delete/112707/non-english Del: in docs -- https://master.php.net/note/delete/112707/in+docs Del: other reasons-- https://master.php.net/note/delete/112707 Reject -- https://master.php.net/note/reject/112707 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195619) next »