Re-implement PHP script engine built-in functions

Source: Internet
Author: User
Tags chr continue encode string implement ord php script strcmp strlen

The following are the referenced contents:

Really boring, whim, want to put some of the PHP inside the package has been provided to reuse PHP implementation again,

So there is the following code mainly implemented in PHP part of the string processing functions, while implementing a number of PHP

No, but the same function of string processing functions can also be used in other languages to actually

Now, like using C/vbscript/perl and so on, you can have a library of your own.

The following functions are not necessarily able to run successfully, just to learn.

//

If no special statement, all due to Heiyeluren original, to use any function, please retain the author information

/**

* String functions Reconstruct

*

* Copyright (c) heiyeluren

* Author:heiyeluren

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

*/

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

*/

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 www.knowsky.com

* @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 $s 1-a-string

* @param string $s 2 second string

* @return int RETURN-1,STR1 < str2; Return 1, str1 > str2, str1 = str2,

* Return 0, the other, return false

* @version v0.1

* @create 2005-5-24

* @modified 2005-5-24

* @author Heiyeluren

*/

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

{

if ($s 1[$i] = = $s 2[$i])

Continue

Else

return false;

}

return 0;

}

// }}}

{{{STRCHR (), Strstr (), Strpos ()

/**

* Find Occurrence a string

*

* @param string $str Parent string

* @param string $substr need match sub string

* @return int return find sub string at parent string

* 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 in PHP, 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

*/

function Str_insert ($str, $i, $SUBSTR)

{

for ($j =0; $j < $i; $j + +)

{

$startstr. = $str [$j];

}

for ($j = $i; $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

{

$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

*/

function strcpy ($s 1, $s 2)

{

if (strlen ($s 1) = = NULL)

Return

if (!isset ($s 2))

Return

for ($i =0; $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

*/

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

*/

/* String encode function */

function Php_encode ($STR)

{

if ($str = = ' && strlen ($str) >128)

return false;

for ($i =0; $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

{

$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 = ' ngzqtcobmuhelkpdawxfyivrsj2468021359 ';

/* String Encrypt function */

function Php_encrypt ($STR)

{

Global $encrypt _key, $decrypt _key;

if (strlen ($str) = = 0)

return false;

for ($i =0; $i

{

for ($j =0; $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

{

for ($j =0; $j

{

if ($str [$i] = = $decrypt _key[$j])

{

$enstr. = $encrypt _key[$j];

Break

}

}

}

return $enstr;

}

// }}}



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.