note 42412 added to function.substr
| From: | biohazard at online dot ge | Date: | Sat, 15 May 2004 17:55:07 +0000 |
| Subject: | note 42412 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69649@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 '['
<?
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);
};
?>
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42412
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42412&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42412&report=yes
Search -- http://master.php.net/manage/user-notes.php