TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   String help! (http://www.talkphp.com/absolute-beginners/2317-string-help.html)

pabin 02-22-2008 07:22 PM

String help!
 
Well i have been trying to extract each chars of a string and keep them in an array. I have used loop to get it but failed. any ideas...
e.g.
Code:

$string = 'hello';
$len = strlen($string);
for($i = 0; $i < $len; $i++){
$char = $string{$i};
//now i am stuck
}


buggabill 02-22-2008 07:52 PM

You would want to do something like this:
PHP Code:

$arr_char = array();
$string 'hello';
$len strlen($string);

for(
$i 0$i $len$i++)
{
    
$arr_char[] = substr($string$i1);


$arr_char will look like this:
PHP Code:

Array
(
    [
0] => h
    
[1] => e
    
[2] => l
    
[3] => l
    
[4] => o



buggabill 02-22-2008 07:53 PM

BTW... welcome to the forums!
You should go introduce yourself over in the new member area.

SOCK 02-22-2008 07:54 PM

You can think about this two ways.

First, you can access the string as if it were an array (which you're attempting to do), e.g.
PHP Code:

$string'hello';
// display the first character in the string, at index 0
echo $string[0]; // 'h' 

In your loop, you would refer to it with the [ ] brackets, not the { } braces. I haven't tested it, but I believe that actually tacks the numeric value of $i to the end of $string at that point in the loop. The other important factor, $char is not defined as an array. It's just a string as well, which is reassigned each time through the loop. I think you want something more like this
PHP Code:

// our string
$string'hello';
// an empty array
$charArray= array();
for ( 
$i=0$istrlen($string); $i++ ) {
  
// assign each character in the string to an empty index
  
$charArray[]= $string[$i];
}
// display the array values
print_r($charArray); 


Kalle 02-22-2008 08:36 PM

In php5 you can say:

PHP Code:

$char_list str_split('TalkPHP');
/**
$char_list = Array('T', 'a', 'l', 'k', 'P', 'H', 'P');
*/ 


DeMo 02-23-2008 12:18 AM

Strings are arrays by default.. you can access each letter directly as SOCK showed.

pabin 02-23-2008 06:44 AM

Thanks for your help. Actually i was trying to make script which would be used for authentication. I thought i would count the length of the password and get each char from password and hash them differently and merge them at end before sending it to the db for authenticaion.

ReSpawN 02-23-2008 09:02 AM

That'll be quite a waste of good resources. Encoding a password which has about 15 letters, would take a lot longer. Than you'll do something like;

PHP Code:

<?php

$stringArray 
= array();
$input 'thisWillbeMyPass';
$length strlen($input);

for(
$i 0$i $length$i++) {
    
$stringArray[] = $input[$i];
}
echo 
'You\'ve stripped '.$i.' words and put them into the array.';
echo 
'<br><b>Total Length:</b> '.$length.' :: <b>Total Stripped:</b> '.$i;

echo 
'<br><br>Your '.$i.' long string put together: ';
for (
$i 0$i $length$i++) {
    echo 
$stringArray[$i];
}

echo 
'<br><br><b>The encrypted one:</b> <br>';
for (
$i 0$i $length$i++) {
    echo 
md5($stringArray[$i]).'&nbsp;&nbsp;&nbsp;['.$stringArray[$i].']<br>';
}

echo 
'<br><br>And of course the total encrypted one: ';
$encrypted '';
for (
$i 0$i $length$i++) {
    
$encrypted .= md5($stringArray[$i]);
}
echo 
'<br><br>Slow, long one: '.md5($encrypted).'<br>Faster but more hackable one: '.md5($input);
?>

Which will output:
Quote:

You've stripped 16 words and put them into the array.
Total Length: 16 :: Total Stripped: 16

Your 16 long string put together: thisWillbeMyPass

The encrypted one:
e358efa489f58062f10dd7316b65649e [t]
2510c39011c5be704182423e3a695e91 [h]
865c0c0b4ab0e063e5caa3387c1a8741 [i]
03c7c0ace395d80182db07ae2c30f034 [s]
61e9c06ea9a85a5088a499df6458d276 [W]
865c0c0b4ab0e063e5caa3387c1a8741 [i]
2db95e8e1a9267b7a1188556b2013b33 [l]
2db95e8e1a9267b7a1188556b2013b33 [l]
92eb5ffee6ae2fec3ad71c777531578f [b]
e1671797c52e15f763380b45e841ec32 [e]
69691c7bdcc3ce6d5d8a1361f22d04ac [M]
415290769594460e2e485922904f345d [y]
44c29edb103a2872f519ad0c9a0fdaaa [P]
0cc175b9c0f1b6a831c399e269772661 [a]
03c7c0ace395d80182db07ae2c30f034 [s]
03c7c0ace395d80182db07ae2c30f034 [s]


And of course the total encrypted one:

Slow, long one: f071e1193cfd4c99d92e1292a70d8f83
Faster but more hackable one: eba11541832ba056a064476ce09afbad
I think that is something you wanted? There surely are easier ways, like using SALT or triple encodes with a mixture of SHA1 and MD5.

pabin 02-23-2008 10:26 AM

Actually i was trying to do the same way but later figured out that this would take longer time if password is long. Well i have been mixing password with salt and hashing with md5 and sha couple of time and reversing the hash or changing the pos of 16 chars of first and last chars of MD5 then only checking with the DB.

ReSpawN 02-23-2008 10:32 AM

Quote:

Originally Posted by pabin (Post 11297)
Actually i was trying to do the same way but later figured out that this would take longer time if password is long. Well i have been mixing password with salt and hashing with md5 and sha couple of time and reversing the hash or changing the pos of 16 chars of first and last chars of MD5 then only checking with the DB.

Also a pretty effective way. Reversing the string has never popped into my thoughts at all. Great idea, good luck non the less. ^^


All times are GMT. The time now is 01:28 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0