Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a basic function

Status
Not open for further replies.

748323

Programmer
Dec 10, 2004
70
US
This function is supposed to switch the string around a bit.

ABCD --> BADC
ABCDE --> BADCE (Even though it doesn't add the last character if the string's length is odd yet, I will add it later).

Code:
<?

function sss($string) {
	for ($i=0; $i <= strlen($string); $i += 2) {
		$chr1 = $string{$i};
		$chr2 = $string{$i + 1};
		$string{$i + 1} = $chr1;
		$string{$i} = $chr2;
	}
	return $string;
}

echo sss($_GET['x']);

?>

That returns nothing, though. Why? Thanks.
 
For some reason PHP inserts a new element into the array when you assign the chars back to the string array. That in turn increases the length of the string and the loop will never end.

Why do you need to treat the string as an array when recomposing? I recommend concatenation of a new string:
Code:
<?

function sss($string) {
    for ($i=0; $i <= strlen($string); $i += 2) {
        $chr1 = $string{$i};
        $chr2 = $string{$i + 1};
        $newstring .= $chr2.$chr1;
    }
    return $newstring;
}

echo sss($_GET['x']);

?>

PHP 5 has str_split which you can use to break the string into pairs of letters - I use substr() here. There is also strrev() to reverse a string.
Code:
function sss($string){
   $i = 0;
   while ($i <= strlen($string)) {
       $diad = substr($string,$i,2);
       $newstr .= strrev($diad);
       $i = $i +2;;
   }
   return($newstr);
}
Odd numbered strings are no trouble with this.

 
Hi,

looks like you made a confusion: you have used { insted of [, the same } against ]

just try:

Code:
function sss($string) {
    for ($i=0; $i < strlen($string)-1; $i += 2) {
        $chr1 = $string[$i];
        $chr2 = $string[$i + 1];
        $string[$i + 1] = $chr1;
        $string[$i] = $chr2;
    }
    return $string;
}

___
____
 
predamarcel
For strings the curly braces are fine, the square braces are kept for compatibility reasons and are deprecated.

The PHP manual states:
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string in curly braces.

Note: For backwards compatibility, you can still use array-brackets for the same purpose. However, this syntax is deprecated as of PHP 4.
 
Thanks a lot. I managed to get passed it. I used arrays, instead of a new variable. I think the function I wrote is longer than it should be, but it still works. Thanks a lot you guys!

Code:
<?php

function sss($string) {
	$stringarray = array();
	$estring = "";

	for($i=0; $i<strlen($string); $i++) {
		$stringarray[] = $string{$i};
	}

	for ($i=0; $i <= count($stringarray); $i += 2) {
		$chr1 = $stringarray[$i];
		$chr2 = $stringarray[$i+1];
		$estring .= $chr2; 
		$estring .= $chr1; 
	}

	if (strlen($estring)/2 != floor(strlen($estring2)/2)) {
		$estring .= $string{count($stringarray)};
	}

	return $estring;
}

echo sss($_GET['x']);

?>
 
man,

Code:
unction sss($string) {
    for ($i=0; $i < strlen($string)-1; $i += 2) {
        $chr1 = $string[$i];
        $chr2 = $string[$i + 1];
        $string[$i + 1] = $chr1;
        $string[$i] = $chr2;
    }
    return $string;
}

WORKS!

Of course you can use {$i} instead of [$i]

___
____
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top