Let's face it, mIRC isn't a very good language once you've started playing with others but the biggest thing you'll miss is the token identifiers, and these are what make mIRC really shine. You'd have thought other languages implemented these too.
Inspired by how NIGathan managed to do it in C++, I decided to write them up for use in PHP, and boy are they useful here.
If you don't know tokens in mIRC you might now know what these are used for. I'll give a few examples below on each one, but all documentation is in comments in the code.
This is a PHP "toolbox" of functions which can be simply include()'d into a PHP code to make your life easier. All of these functions return values.
Note:: findtok(), reptok() and wildtok() are still trying to be figured out. If you have any suggestions on how to do it, please leave a comment!
Thank you to Viper-7 on freenode who helped me create matchtok(), that one was really a bitch.
gettok: gettok("apple orange pear cherry melon", " ", 4) --> cherry
numtok: numtok("chips-burger-pizza", "-") --> 3
sorttok: sorttok("cherry pineappe banana", "r", " ") --> banana cherry pineapple. Note that you can specify the r flag (regular) or n flag (numeric).
addtok: addtok("cherry|banana|peach|orange", "melon", "|") --> cherry|banana|peach|orange|melon
deltok: deltok("a.b.c.d", 3, ".") --> a.b.d
istok: istok("banana orange pear", "pear", " ") --> true
puttok: puttok("a.b.c.d", "e", 2, ".") --> a.e.c.d
remtok: remtok("a.b.c.d", "b", 1, ".") --> a.c.d
lasttok: lasttok("apple cherry banana", " ") --> banana
firsttok: firsttok("apple cherry banana", " ") --> apple
randtok: randtok("apple_cherrybanana", "") --> could return apple, cherry, or banana randomly
matchtok:
matchtok("one two three", "e", 0, " ") --> 2
matchtok("one two three", "e", 2, " ") --> three
instok: instok("a.b.d", "c", 3, ".") --> a.b.c.d
<?php
# Get token. Probably the most handy one out there:
# gettok(string, delimiter, N)
# Examples:
#
# gettok("How are you?", " ", 2) ---> "are"
# gettok("apple|orange|cherry|peach|melon", "|", 5) --> "melon"
function gettok($t, $c, $n) {
$explode = explode($c, $t);
$num = $n - 1;
if (!isset($explode[$num])) {
return false;
}
else {
return $explode[$num];
}
}
# Number of tokens.
# numtok(string, delimiter)
# Examples:
#
# numtok("chips burger pizza", " ") --> 3
# numtok("elephant~cow~sheep~camel", "~") --> 4
function numtok($t, $c) {
$explode = explode($c, $t);
return count($explode);
}
# Sort string by tokens. r flag is regular, n is numeric.
# sorttok(string, flags, delimiter)
# Examples:
#
# sorttok("cherry pineapple banana", "r", " ") --> "banana cherry pineapple"
function sorttok($t, $f, $c) {
$explode = explode($c, $t);
if ($f == "r") {
sort($explode, SORT_REGULAR);
return implode($c, $explode);
}
if ($f == "n") {
sort($explode, SORT_NUMERIC);
return implode($c, $explode);
}
}
# Adds token to string if token does not already exist.
# addtok(string, token, delimiter)
# Examples:
#
# $blah = addtok("cherry banana peach orange", "melon", " ") --> "cherry banana peach orange melon"
# $blah = addtok("cherry banana peach orange", "cherry", " ") --> "cherry banana peach orange"
function addtok($t, $k, $c) {
$explode = explode($c, $t);
foreach ($explode as $val) {
if ($val == $k) {
$o = 1;
break;
}
}
if ($o == 1) {
return implode($c, $explode);
}
else {
$arnum = count($explode) + 1;
$explode[$arnum] = $k;
return implode($c, $explode);
}
}
# Reverse of addtok. Deletes the Nth token from string.
# deltok(string, N, delimiter)
# Examples:
#
# deltok("a.b.c.d", 3, ".") --> "a.b.d"
function deltok($t, $n, $c) {
$explode = explode($c, $t);
$num = $n - 1;
unset($explode[$num]);
return implode($c, $explode);
}
# Returns true or false if the token exists in string.
# istok(string, token, delimiter)
# Examples:
#
# istok("banana orange pear", "pear", " ") --> true
# istok("banana orange pear", "grape", " ") --> false
function istok($t, $k, $c) {
$explode = explode($c, $t);
foreach ($explode as $val) {
if ($val == $k) {
$o = true;
break;
}
}
if ($o == true) {
return true;
}
else {
return false;
}
}
# Overwrites the Nth token in string with a new token.
# puttok(string, token, N, delimiter)
# Examples:
#
# puttok("a.b.c.d", "e", 2, ".") --> a.e.c.d
function puttok($t, $k, $n, $c) {
$explode = explode($c, $t);
$num = $n - 1;
$rep = array($num => $k);
$arep = array_replace($explode, $rep);
return implode($c, $arep);
}
# Removes the Nth matching token from string. Almost like deltok() but with a twist. N is the number of matches to remove.
# remtok(string, token, N, delimiter)
# Examples:
#
# remtok("a.b.c.d", "b", 1, ".") --> "a.c.d"
# remtok("a.b.c.d.b", "b", 0, ".") --> "a.c.d"
function remtok($text, $token, $n, $c) {
$tokens = explode($c, $text);
$keys = array_keys($tokens, $token);
if ($n > 0) {
--$n;
if (count($keys) > $n) {
unset($tokens[$keys[$n]]);
}
}
else {
foreach ($keys as $key) {
unset($tokens[$key]);
}
}
return implode($c, $tokens);
}
# Returns last token in string.
# lasttok(string, delimiter)
# Examples:
#
# lasttok("apple cherry banana", " ") --> "banana"
function lasttok($t, $c) {
$explode = explode($c, $t);
$end = end($explode);
return $end;
}
# The reverse of lasttok, returns the first token in string.
# firsttok(string, delimiter)
# Examples:
#
# firsttok("apple cherry banana", " ") --> "apple"
function firsttok($t, $c) {
$explode = explode($c, $t);
return $explode[0];
}
# Returns a random token from a string.
# randtok(string, delimiter)
# Examples:
#
# randtok("apple cherry banana", " ") --> could return apple, cherry, or banana randomly
function randtok($t, $c) {
$rnum = rand(1, numtok($t, $c));
return gettok($t, $c, $rnum);
}
# Returns tokens that contain the specified string. If N is 0, returns total number of matches. If N is not 0, it will return the Nth matching token.
# matchtok(string, tokens, N, delimiter)
# Examples:
#
# matchtok("one two three", "e", 0, " ") --> 2
# matchtok("one two three", "e", 2, " ") --> three
function matchtok($text, $search, $returnmatch = 1, $delimiter = ' ') {
$i = 0;
$token = strtok($text, $delimiter);
do {
if(strpos($token, $search) !== FALSE) {
$i++;
if($i == $returnmatch) {
return $token;
}
}
}
while(($token = strtok($delimiter)) !== FALSE);
return $i;
}
# Inserts token into the Nth position in text. Like puttok, but instead "adds" it in and doesn't overwrite the token.
# instok(string, token, N, delimiter)
# Examples:
#
# instok("a.b.d", "c", 3, ".") --> "a.b.c.d"
# instok("a.b.d", "c", 9, ".") --> "a.b.d.c"
# instok("apple cherry peach", "tomato", 2, " ") --> "apple tomato cherry peach"
function instok($t, $k, $n, $c) {
$explode = explode($c, $t);
$num = $n - 1;
$first = array_slice($explode, 0, $num);
$last = array_slice($explode, $num);
$anum = count($first) + 1;
$first[$anum] = $k;
$final = array_merge($first, $last);
return implode($c, $final);
}
?>
using RJosh's example i got this
function gettok($string,$n,$c) {
if(is_numeric($c)) $c=chr($c);
$tok=explode($c,$string);
if($n==0) return count($tok);
elseif($n<0) {
$tok=array_reverse($tok);
return $tok[lasttok($n+1,"-")];
}
elseif(preg_match("/(\d+)-(\d+)?/",$n,$range)) {
$start=$range[1]-1;
if (!$range[2]) $end=numtok($string,$c);
else $end=$range[2];
for(;$start<$end;$start++,$text.=$tok[$start-1].$c);
return $text;
}
else return $tok[$n-1];
}
supports negative and multiple ranges
It doesn't look like you have support for ranges or negative values.
IE:
gettok("hello there what's up",2-3," ") -> there what's
gettok("hello there what's up",-1," ") -> up
Maybe something you could look into. I had made a gettok that supported ranges a while back. i'll try and find it or remake it .
EDIT:
I took the liberty of doing my best to remake what i had and this is what i came up with.
function gettok($string,$n,$c) {
if(is_numeric($c)) {
$c = chr($c);
}
$tok = explode($c,$string);
if($n==0) {
return count($tok);
}
if($n < 0) {
$n = count($tok)-$n;
}
if(@preg_match("#(\d+)-(\d+)?#",$n,$range)) {
$text = "";
if(count($range[1])==1) {
$r1 = $range[1][0]-1;
$r2 = count($tok);
} else {
$r1 = $range[1][0]-1;
$r2 = $range[1][1];
}
while($r1<$r2) {
$text .= $tok[$r1];
$r1++;
}
return $text;
}
if(empty($tok[$n-1])) {
return FALSE;
} else {
return $tok[$n-1];
}
}
function findtok($text,$token,$n,$c) {
$text=explode($c,$text);
foreach ($text as $num => $match)
if ($match==$token) $all.=$num+1 ." ";
$a=numtok($all," ");
if ($n!=0&&$n<=$a) return gettok($all,$c,$n);
else return $a-1;
}
function reptok ($text,$token,$new,$n,$c) {
$text=explode($c,$text);
foreach ($text as $num => $match) {
if ($match==$token) {
$a++;
if ($a==$n||$n==0) $text[$num]=$new;
}
}
return implode($c,$text);
}
function regtok($text,$string,$n,$c) {
$text=explode($c,$text);
foreach ($text as $num => $match)
if (preg_match($string,$match)) $all.=$num+1 ." ";
$a=numtok($all," ");
if ($n!=0&&$n<=$a) return gettok($all,$c,$n);
else return $a-1;
}
they all follow same syntax as mIRC
you can use regtok for wildtok just add a '/' before and after the wildcard string