Use php to implement PHP script engine built-in functions again

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

// It's really boring. It's a strange idea. I want to re-use PHP to implement some encapsulated functions in PHP,
// The following code mainly implements some string processing functions in PHP, and implements some PHP
// None, but the same function of string processing functions can also be implemented in other languages.
// Now, such as using C/VBScript/Perl, you can have your own function library.
// The following functions may not be able to run successfully, just for learning.
//
// If there is no special declaration, it is all because heiyeluren is original. To use any function, keep the author information.

/**
* String Functions Reconstruct
*
* Copyright (c) 2005 heiyeluren * Author: heiyeluren

* $ Id: StringFunctions. php, v 0.1 e 2005-5-29 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 */
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 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 */
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 */
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 $ s1 first string
* @ Param string $ s2 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 */
Function strcmp1 ($ s1, $ s2)
{
If (strlen ($ s1) <strlen ($ s2 ))
Return-1;
If (strlen ($ s1)> strlen ($ s2 ))
Return 1;

For ($ I = 0; $ I <strlen ($ s1); $ I ++)
{
If ($ s1 [$ I] ==$ s2 [$ 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 parent string first place,
* F not find, return false
* @ Version v0.4
* @ Create 2005-5-24
* @ Modified 2005-5-29
* @ Author heiyeluren */
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 */
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 found in PHP, play ***************/

// {Insert_str (), delete_str (), index_str ()
/**
* Basic string operate
*
* @ Param string $ str need get sub string variable
* @ Param int $ 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 */
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 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 */
Function strcpy ($ s1, $ s2)
{
If (strlen ($ s1) = NULL)
Return;
If (! Isset ($ s2 ))
Return;

For ($ I = 0; $ I <strlen ($ s1); $ I ++)
{
$ S2 [] = $ s1 [$ I];
}
Return $ s2;
}
//}}}

// {Strcat ()
/**
* Use designate sub string replace string
*
* @ Param string $ str need get sub string variable
* @ Param int $ 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 */
Function strcat ($ s1, $ s2)
{
If (! Isset ($ s1 ))
Return;
If (! Isset ($ s2 ))
Return;

$ Newstr = $ s1. $ s2;

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 */

/* 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 */

/* Define crypt key */
$ Encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
$ Decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468038559 ';

/* 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;
}
//}}}

If the code is not clear, check the connection. It is my blog in blogchina:

Http://heiyeluren.blogchina.com/1741318.html

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.