Using PHP to re-implement the PHP script engine built-in functions

Source: Internet
Author: User
Tags count string length crypt decrypt encode string first string ord strcmp

Really bored, whim, want to put the PHP inside part already provide encapsulated function re-use PHP implementation again,
So the following code is mainly implemented in PHP part of the string processing function, while the implementation of some PHP
No, but the same function as the string handler function, can also be used in other languages to actually
Now, for example with C/vbscript/perl and so on, then you can have your own library of functions.
The following functions do not necessarily run successfully, just to learn.
//
If there is no special statement, all due to Heiyeluren original, to use any function, please retain the author's information


/**
* String Functions Reconstruct
*
* Copyright (c) 2005 Heiyeluren <[email protected]>
* Author:heiyeluren <[email protected]>

* $Id: Stringfunctions.php,v 0.1 e 2005-5-29 23:21 heiyeluren EXP $
**/


{{{strlen ()}
/**
* Count String length
*
* @param string $str need count length string variable
* @return int return count result
* @version v0.1
* @create 2005-5-24
* @modified 2005-5-24
* @author Heiyeluren <[email protected]>
*/
function Strlen1 ($STR)
{
if ($str = = ")
return 0;

$count = 0;
while (1)
{
if ($str [$count]! = NULL)
{
$count + +;
Continue
}
Else
Break
}
return $count;
}
// }}}


{{{substr ()}
/**
* Get Sub string
*
* @param string $str need get sub string variable
* @param int $start start get sub string
* @param int $length need get string length
* @return String Return sub string
* @version v0.2
* @create 2005-5-24
* @modified 2005-5-25
* @author Heiyeluren <[email protected]>
*/
function Substr1 ($str, $start, $length =0)
{
if ($str = = ")
Return
if ($start > strlen ($STR))
Return
if ($length! = NULL) && ($start > 0) && ($length > strlen ($STR)-$start))
Return
if ($length! = NULL) && ($start < 0) && ($length > strlen ($str) + $start))
Return

if ($length = = NULL)
$length = (strlen ($str)-$start);

if ($start < 0)
{
for ($i = (strlen ($str) + $start), $i < (strlen ($STR) + $start + $length), $i + +)
{
$substr. = $str [$i];
}
}

if ($length > 0)
{
for ($i = $start; $i < ($start + $length); $i + +)
{
$substr. = $str [$i];
}
}

if ($length < 0)
{
for ($i = $start, $i < (strlen ($STR) + $length), $i + +)
{
$substr. = $str [$i];
}
}
return $substr;
}
// }}}


{{{strrev ()}
/**
* Reversal String Order
*
* @param string $STR need reversal string variable
* @return String Reversal string
* @version v0.1
* @create 2005-5-24
* @modified 2005-5-24
* @author Heiyeluren <[email protected]>
*/
function Strrev1 ($STR)
{
if ($str = = ")
return 0;
for ($i = (strlen ($str)-1); $i >=0; $i-)
{
$rev _str. = $str [$i];
}
return $rev _str;
}
// }}}


{{{strcmp ()}
/**
* String Comparison
*
* @param string $s 1 first string
* @param string $s 2 second string
* @return int RETURN-1,STR1 < str2; Return 1, str1 > str2, str1 = str2,
* Return 0, other, return false
* @version v0.1
* @create 2005-5-24
* @modified 2005-5-24
* @author Heiyeluren <[email protected]>
*/
function Strcmp1 ($s 1, $s 2)
{
if (strlen ($s 1) < strlen ($s 2))
return-1;
if (strlen ($s 1) > strlen ($s 2))
return 1;

for ($i =0; $i <strlen ($s 1); $i + +)
{
if ($s 1[$i] = = $s 2[$i])
Continue
Else
return false;
}
return 0;
}
// }}}


{{{STRCHR (), Strstr (), Strpos ()
/**
* Find first occurrence of a string
*
* @param string $str Parent string
* @param string $substr need match sub string
* @return int return find sub string at the parent string first place,
* F not find, return false
* @version v0.4
* @create 2005-5-24
* @modified 2005-5-29
* @author Heiyeluren <[email protected]>
*/
function Strchr1 ($STR, $SUBSTR)
{
$m = strlen ($STR);
$n = strlen ($SUBSTR);

if ($m < $n)
return false;

for ($i =0; $i <= ($m-$n + 1); $i + +)
{
$sub = substr ($str, $i, $n);
if (strcmp ($sub, $substr) = = 0)
return $i;
}
return false;
}
// }}}


{{{str_replace ()}
/**
* Replace all occurrences of the search string with the replacement string
*
* @param string $substr need replace sub string variable
* @param string $newsubstr New sub String
* @param string $STR operate parent string
* @return string return replace after new parent string
* @version v0.2
* @create 2005-5-24
* @modified 2005-5-29
* @author Heiyeluren <[email protected]>
*/
function Str_replace1 ($substr, $newsubstr, $STR)
{
$m = strlen ($STR);
$n = strlen ($SUBSTR);
$x = strlen ($NEWSUBSTR);

if (STRCHR ($str, $substr) = = False)
return false;

for ($i =0; $i <= ($m-$n + 1); $i + +)
{
$i = STRCHR ($str, $SUBSTR);
$str = Str_delete ($str, $i, $n);
$str = Str_insert ($str, $i, $NEWSTR);
}
return $str;
}
// }}}


/************ the following string processing functions are not in PHP, and write their own play ***************/

{{{insert_str (), Delete_str (), Index_str ()
/**
* Basic string operate
*
* @param string $str need get sub string variable
* @param int $start start get sub string
* @param int $length need get string length
* @return String Return sub string
* @version v0.1
* @create 2005-5-24
* @modified 2005-5-24
* @author Heiyeluren <[email protected]>
*/
function Str_insert ($str, $i, $SUBSTR)
{

for ($j =0; $j < $i; $j + +)
{
$startstr. = $str [$j];
}

for ($j = $i; $j <strlen ($STR); $j + +)
{
$laststr. = $str [$j];
}
$str = ($startstr. $substr. $laststr);

return $str;
}


function Str_delete ($str, $i, $j)
{
for ($c =0; $c < $i; $c + +)
{
$startstr. = $str [$c];
}

for ($c = ($i + $j), $c <strlen ($STR); $c + +)
{
$laststr. = $str [$c];
}

$str = ($startstr. $laststr);

return $str;
}
// }}}


{{{strcpy ()}
/**
* Use designate sub string Replace string
*
* @param string $str need get sub string variable
* @param int $start start get sub string
* @param int $length need get string length
* @return String Return sub string
* @version v0.1
* @create 2005-5-27
* @modified 2005-5-27
* @author Heiyeluren <[email protected]>
*/
function strcpy ($s 1, $s 2)
{
if (strlen ($s 1) = = NULL)
Return
if (!isset ($s 2))
Return

for ($i =0; $i <strlen ($s 1); $i + +)
{
$s 2[] = $s 1[$i];
}
return $s 2;
}
// }}}


{{{strcat ()}
/**
* Use designate sub string Replace string
*
* @param string $str need get sub string variable
* @param int $start start get sub string
* @param int $length need get string length
* @return String Return sub string
* @version v0.1
* @create 2005-5-27
* @modified 2005-5-27
* @author Heiyeluren <[email protected]>
*/
function strcat ($s 1, $s 2)
{
if (!isset ($s 1))
Return
if (!isset ($s 2))
Return

$newstr = $s 1. $s 2;

return $newsstr;
}
// }}}


{{{Php_encode (), Php_decode ()
/**
* Simple string Encode/decode function
*
* @param string $str need code/encode string variable
* @return string Code/encode after string
* @version v0.2
* @create 2005-3-11
* @modified 2005-5-24
* @author Heiyeluren <[email protected]>
*/

/* String encode function */
function Php_encode ($STR)
{
if ($str = = "&& strlen ($STR) >128)
return false;

for ($i =0; $i <strlen ($STR); $i + +)
{
$c = Ord ($str [$i]);
if ($c >31 && $c <107)
$c + = 20;
if ($c >106 && $c <127)
$c-= 75;
$word = Chr ($c);

$s. = $word;
}
return $s;
}


/* String decode function */
function Php_decode ($STR)
{
if ($str = = "&& strlen ($STR) >128)
return false;

for ($i =0; $i <strlen ($STR); $i + +)
{
$c = Ord ($word);
if ($c >106 && $c <127)
$c = $c-20;
if ($c >31 && $c <107)
$c = $c +75;
$word = Chr ($c);

$s. = $word;
}
return $s;
}
// }}}


{{{php_encrypt (), Php_decrypt ()
/**
* Simple string Encrypt/decrypt function
*
* @param string $str need crypt string variable
* @return string Encrypt/decrypt after string
* @version v0.1
* @create 2005-5-27
* @modified 2005-5-29
* @author Heiyeluren <[email protected]>
*/

/* Define crypt Key */
$encrypt _key = ' abcdefghijklmnopqrstuvwxyz1234567890 ';
$decrypt _key = ' ngzqtcobmuhelkpdawxfyivrsj2468021359 ';

/* String Encrypt function */
function Php_encrypt ($STR)
{
Global $encrypt _key, $decrypt _key;

if (strlen ($str) = = 0)
return false;

for ($i =0; $i <strlen ($STR); $i + +)
{
for ($j =0; $j <strlen ($encrypt _key); $j + +)
{
if ($str [$i] = = $encrypt _key[$j])
{
$enstr. = $decrypt _key[$j];
Break
}
}
}
return $enstr;
}


/* String Decrypt function */
function Php_decrypt ($STR)
{
Global $encrypt _key, $decrypt _key;

if (strlen ($str) = = 0)
return false;

for ($i =0; $i <strlen ($STR); $i + +)
{
for ($j =0; $j <strlen ($decrypt _key); $j + +)
{
if ($str [$i] = = $decrypt _key[$j])
{
$enstr. = $encrypt _key[$j];
Break
}
}
}
return $enstr;
}
// }}}
 

Using PHP to re-implement the PHP script engine built-in functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.