Dynamic Web technology PHP program string processing function

Source: Internet
Author: User
Tags chr continue count decrypt ord strcmp strlen

?
//------------------------
PHP Built-in string function implementation
//------------------------

String length
function strlen ($STR)
{
if ($str = = ") return 0;
$count = 0;
while (1) {
if ($str [$count]!= NULL) {
$count + +;
Continue
}else{
Break
}
}
return $count;
}

To intercept a child string
function substr ($str, $start, $length =null)
{
if ($str = = ' | | $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;
}

String Flip
function Strrev ($STR)
{
if ($str = = ") return 0;
for ($i = (strlen ($str)-1); $i >=0; $i-) {
$rev _str. = $str [$i];
}
return $rev _str;
}

String comparisons
function strcmp ($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;
}

Find string
function Strstr ($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;
}

String substitution
function Str_replace ($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;
}
?>

<?php
//--------------------
Self-Implementation String handler function
//--------------------

Insert a section of string
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;
}

//delete a section of string
function Str_delete ($str, $i, $j)
{
        for ($c =0; $c &L t; $i;  $c + +) {
                 $STARTSTR. = $str [$c];
       }
        for ($c = ($i + $j); $c <strlen ($STR); $c + +) {
                 $laststr  . = $str [$c];
       }
         $str = ($startstr. $laststr);
        return $str;
}

Copy string
function strcpy ($s 1, $s 2)
{
if (strlen ($s 1) ==null | |!isset ($s 2)) return;
for ($i =0; $i <strlen ($s 1); $i + +) {
$s 2[] = $s 1 [$i];
}
return $s 2;
}

Connection string
function strcat ($s 1, $s 2)
{
if (!isset ($s 1) | | |!isset ($s 2)) return;
$newstr = $s 1;
for ($i =0; $i <count ($s); $i + +) {
$newstr. = $st [$i];
}
return $newsstr;
}

Simple encoding function (corresponding to the Php_decode 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;
}

Simple decoding function (corresponding to the Php_encode 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 <) $c = $c +75;
$word = Chr ($c);
$s. = $word;
}
return $s;
}

Simple cryptographic functions (corresponding to the Php_decrypt function)
function Php_encrypt ($STR)
{
$encrypt _key = ' abcdefghijklmnopqrstuvwxyz1234567890 ';
$decrypt _key = ' ngzqtcobmuhelkpdawxfyivrsj2468021359 ';
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;
}

Simple decryption function (corresponding to the Php_encrypt function)
function Php_decrypt ($STR)
{
$encrypt _key = ' abcdefghijklmnopqrstuvwxyz1234567890 ';
$decrypt _key = ' ngzqtcobmuhelkpdawxfyivrsj2468021359 ';
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;
}
?>



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.