圣斗士的信仰
注册会员

UID 84775
精华
0
积分 74
帖子 76
金钱 74 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-12-13
状态 离线
|
[广告]: q
m
无字符串和数组处理函数的字符串反转.
//'This is a book' 反转后->'book a is This'
<?php
$foo = 'This is a book';
$i = 0;
$result = '';
$temp = '';
while(isset($foo{$i}))
{
if ($foo{$i} === ' ')
{
$result = ' ' . $temp . $result;
$temp = '';
}
else
{
$temp .= $foo{$i};
}
$i ++;
}
$result = $temp . $result;
echo $result;
?>
<?php
$str = 'quick fox jumps over lazy dog';
$res = '';
$exp = array();
function my_count($str)
{
$i = 0;
while (isset($str[$i])) {
++$i;
}
return $i;
}
for ($i = 0, $j = 0, $max = my_count($str); $i < $max; ++$i) {
if ($str[$i] == ' ') {
++$j;
continue;
}
$exp[$j] .= $str[$i];
}
for ($i = my_count($exp) - 1; $i >= 0; --$i) {
if ($i == 0) {
$res .= "{$exp[$i]}";
} else {
$res .= "{$exp[$i]} ";
}
}
var_dump($res);
?>
<?php
$str="This is a book";
$result='';
$chars = preg_split('/ /', $str);
foreach($chars as $value)
{
$result = $value . ' ' . $result;
}
print $result;
?>
<?
$str = 'quick fox jumps over lazy dog';
$res = '';
$tmp = '';
$i = 0;
while (($str[$i] || $str[$i-1]) && $tmp .= $str[$i++]) {
if ($str[$i-1] == ' ' || $str[$i-1] == NULL) {
$res = $str[$i-1] == NULL ? "$tmp $res" : "$tmp$res";
$tmp = '';
}
}
echo $res;
?>
<?php
$str = 'quick fox jumps over lazy dog';
$res = '';
$tmp = '';
$i = $first = 0;
while ((isset($str[$i]) || isset($str[$i-1])) &&
($tmp = (isset($str[$i]) && $str[$i] == ' ') ? $tmp : "{$tmp}{$str[$i]}")) {
++$i;
if ((isset($str[$i-1]) && $str[$i-1] == ' ') || !isset($str[$i-1])) {
($first == 0) ? (($first = 1) && $res = $tmp) : $res = "$tmp $res";
$tmp = '';
}
}
var_dump($res);
?>
<?
$str = 'quick fox jumps over lazy doggie';
$res = $str;
while ($str[$len++]);
for ($i = $len-1, $j = 0; $i >= 0; --$i) {
if (($str[$i]==' ' and $k=$i+1) || ($i==0 and !($k=$i))) {
while ((isset($str[$k]) && $str[$k] != ' ') && $res[$j++] = $str[$k++]);
$j+1 <= $len-1 and $res[$j++] = ' ';
}
}
var_dump($res);
?>
<?php
$foo = 'This is a book';
function bar($foo)
{
$i = 0;
$result = '';
$temp = '';
while(isset($foo{$i}))
{
if ($foo{$i} === ' ')
{
$result = ' ' . $temp . $result;
$temp = '';
}
else
{
$temp .= $foo{$i};
}
$i ++;
}
$result = $temp . $result;
return $result;
}
echo bar($foo);
?>
<?
$s='this is a book';
for($i=0,$m=$n='';isset($s[$i]);$i++)
{
if($s[$i]==" "){$m=$n.' '.$m;$n='';}else{$n.=$s[$i];}
}
echo $n.' '.$m;
?>
<?php
$result = '';
$return = '';
$a='This is a book';
$i = 0;
function turn( $str, $length= 0 )
{
global $result, $return;
$tmp = '';
$i = $length;
for(;; $i++ )
{
if ( isset($str{$i}) )
if ($str{$i} == " " )
{
$result = $tmp . ' ' . $result;
turn( $str, $i + 1 );
}
else
$tmp .= $str{$i};
else
{
if ( $return == '' )
$return = $tmp . ' ' . $result;
else
break;
}
}
}
turn( $a );
echo $return;
?>
<?php
function turn1( $str )
{
$return = '';
$tmp = '';
for(;; $i++ )
{
if ( isset( $str{$i} ) )
{
$tmp .= $str{$i};
if ( $str{$i} == " " )
{
$result = $tmp . $result;
$tmp = '';
}
}
else
return $tmp . ' ' . $result;
}
}
echo turn1('This is a book');
?>
<?php
$str = 'This is a book';
$i = 0;
function fun($i)
{
global $str;
$k = $i;
for(;; $k-- )
{
if(!isset($str[$k]) || $str[$k]==' ')
{
for($j = $k+1;$j <= $i; $j++ )print $str[$j];
if(!isset($str[$k]))return;
else
{
print " ";
return fun($k-1);
}
}
}
}
while (isset($str[$i])) {
$i++;
}
fun($i-1);
?>
<?
$string = 'This is a book';
$string = $string . ' ';
$i = 0;
while( isset( $string[$i] ) )
{
if( $string[$i] == ' ' )
{
$word = $s . ' ' . $word;
$s = '';
}else
{
$s .= $string[$i];
}
$i++;
}
echo $word;
?>
|
|