extract sub parts from strings in php
26. 4. 2012may 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);
};
suche nach Textstellen in eine Datei leiten
25. 4. 2012Zusammen mit find, grep und tee kann man ganze Verzeichnisbäume durchsucht und das Ergebnis in eine Textdatei geschrieben werden. Man sollte nur darauf achten, wenn man die Textdatei so benennt wie den Suchbegriff, dass dann die Textdatei in einem tieferem Verzeichnis liegt. Sonst entstehen Rekursionen.
find ./ -exec grep -H "versandliste" {} \; | tee ../suchgrep/versandliste.txt


