MD5'ing a bunch at once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

MD5'ing a bunch at once

Post by HiddenS3crets »

What I am trying to do is input a list of words, then have PHP code create a MD5 equivalent of each, then reformat the line to look like:

MD5:real_word

To go about this I would need to read the input one line at a time, create a MD5 equivalent, modify the line, then go on to the next line.

sample input:
cat
dog
people

How can I go from cat to dog, then dog to people?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a loop. :?
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post by HiddenS3crets »

Lol, but how specifically? Is there a function to move from one line to another or something?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not specifically. How are you getting these lines? From a file? From a form?

Files would use file() or siblings functions while forms would use explode() to break apart the data into individual elements of an array. A basic foreach loop can be used from there.
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post by HiddenS3crets »

It comes from a form. I'll check out explode(), thanks
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post by HiddenS3crets »

I ended up solving this a different way... this is my code

Code: Select all

<?php
$str = $_POST['str'];
$length = str_word_count($str);

if(isset($str)) {

     for($x = 0; $x < $length; $x++) {
        $ind = explode("\n", $str);
        $md5 = md5($ind[$x]);

        echo $md5 . ":" . $ind[$x] . "<br>";
     }
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

exploding the string repeatedly would just wasted processing time.. store the results of the explosion.. and use count() on the exploded data..
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

What feyd is suggesting, is something like:

Code: Select all

<?php

$str = explode("\n", $_POST['str']);
$md5str = array();

foreach ($str as $val) {
    $md5 = md5($val);
    $md5str[] = $md5;
    echo "$md5:$val<br />\n";
}

?>
So now you have the original text in the array $str, and the md5 hashed text in the array $md5str.
Post Reply