Showing posts with label String Manipulation. Show all posts
Showing posts with label String Manipulation. Show all posts

Wednesday, March 3, 2010

PHP Function Number to Roman and Roman to Number


class konversi{
function roman2number($roman){
$conv = array(
array("letter" => 'I', "number" => 1),
array("letter" => 'V', "number" => 5),
array("letter" => 'X', "number" => 10),
array("letter" => 'L', "number" => 50),
array("letter" => 'C', "number" => 100),
array("letter" => 'D', "number" => 500),
array("letter" => 'M', "number" => 1000),
array("letter" => 0, "number" => 0)
);
$arabic = 0;
$state = 0;
$sidx = 0;
$len = strlen($roman);

while ($len >= 0) {
$i = 0;
$sidx = $len;

while ($conv[$i]['number'] > 0) {
if (strtoupper($roman[$sidx]) == $conv[$i]['letter']) {
if ($state > $conv[$i]['number']) {
$arabic -= $conv[$i]['number'];
} else {
$arabic += $conv[$i]['number'];
$state = $conv[$i]['number'];
}
}
$i++;
}

$len--;
}

return($arabic);
}


function number2roman($num,$isUpper=true) {
$n = intval($num);
$res = '';

/*** roman_numerals array ***/
$roman_numerals = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1);

foreach ($roman_numerals as $roman => $number)
{
/*** divide to get matches ***/
$matches = intval($n / $number);

/*** assign the roman char * $matches ***/
$res .= str_repeat($roman, $matches);

/*** substract from the number ***/
$n = $n % $number;
}

/*** return the res ***/
if($isUpper) return $res;
else return strtolower($res);
}
}

Read More...

PHP Substr In Array

To find if a value exists in an array, the PHP in_array() function works quite nicely. But there are times when only a partial match is required to check in the array. This substr_in_array() function checks will search the values of an array for a substring. The $needl can be a string or an array of strings to search for.


<?php
/**
*
* @Search for substring in an array
*
* @param string $neele
*
* @param mixed $haystack
*
* @return bool
*
*/
function substr_in_array($needle, $haystack)
{
/*** cast to array ***/
$needle = (array) $needle;

/*** map with preg_quote ***/
$needle = array_map('preg_quote', $needle);

/*** loop of the array to get the search pattern ***/
foreach ($needle as $pattern)
{
if (count(preg_grep("/$pattern/", $haystack)) > 0)
return true;
}
/*** if it is not found ***/
return false;
}
?>


Example Usage


<?php

/*** an arrray to search through ***/
$array = array('dingo', 'wombat', 'kangaroo', 'platypus');


/*** an array of values to search for ***/
$strings = array('foo', 'bar', 'kang');

/*** check for true or false with ternary ***/
echo substr_in_array( $strings, $array ) ? 'found' : 'not found';

/*** a single string to search for ***/
$string = 'plat';

/*** check for true or false with ternary ***/
echo substr_in_array( $string, $array ) ? 'found' : 'not found';
?>

Read More...

Friday, November 7, 2008

Delphi Encrypt - Decrypt Functions

To encrypt / decrypt strings without including "bad" characters (spaces, car return, line feed, tabs, etc)

Code :

function TCrypt.Desencripta(const S: String): String;
var
I: byte;
Key: Word;
ls : string;
begin
Key := 1674;
SetLength(ls,Length(S) div 2);
SetLength(Result,Length(ls));
for I := 1 to Length(ls) do begin
ls[I] := char(StrToInt('$'+ Copy(S, (I*2)-1 , 2)));
end;

for I := 1 to Length(ls) do begin
Result[I] := char(byte(ls[I]) xor (Key shr 8));
Key := (byte(ls[I]) + Key) * C1 + C2;
end;
end;


function TCrypt.Encripta(const S: String): String;
var
I: byte;
Key: Word;
ls : string;
begin
Key := 1674;
SetLength(ls,Length(S));
Result := '';
for I := 1 to Length(S) do begin
ls[I] := char(byte(S[I]) xor (Key shr 8));
Result := Result + IntToHex(byte(ls[I]),2);
Key := (byte(ls[I]) + Key) * C1 + C2;
end;



Download Code
Source : Planet-Source-Code.com

Read More...

Latest Comments

About Me

My photo
Makassar, Sulawesi Selatan, Indonesia

Guest Book


ShoutMix chat widget

Script Sense ©Template Blogger Green by Dicas Blogger.

TOPO