Results 1 to 3 of 3

Thread: [RESOLVED] Using | to split words?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    190

    Resolved [RESOLVED] Using | to split words?

    I'm using a code that allows users to add a message to a text file, the message shows in a image but at the end of every message theres a character that isn't supposed to be there (I think its because the code makes a new line for every new message). Does anyone know how to fix? If not then can someone tell me how to make it split messages using | instead of a new line?

    This is the code that lets people add a message:-


    PHP Code:
    <?php

    if (isset($newmsg)) { 
    // go through the proccess of checking, and possibly adding a new  message

    $file "messages.txt";      // message file
    $fd fopen($file"a+");
    $wordarray file($file);


    $newmsg stripslashes($newmsg);
    $newmsg strip_tags($newmsg);

    if (!
    in_array($newmsg."\r\n",$wordarray,TRUE)) { // word not already there

    if (!$newmsg=="") { // don't accept empty message
        
    fputs($fd,$newmsg."\r\n");
        echo(
    "<h4>The message \"$newmsg\" has been added!</h4>");

    // Now the logging, for people who use this to insult others.

    $ip=$_SERVER['REMOTE_ADDR'];   // Get IP
    $date=date('h:i:s A | D | F dS'); // Get date

    // Below is the path and what to put in log.htm

    $logtxt="<html><body bgcolor=black><tt><font color=red>$ip</font> <font color=blue>| $date | </font> <font color=white> $newmsg</font></font></tt><BR><hr><BR>";
    $fp=fopen("addlog.htm""a+");   // Open addlog.htm  
    fwrite($fp$logtxt); // Write to addlog.htm
    fclose($fp);  // Close addlog.htm

        
    }
    else {
        echo 
    "<h4>You cannot enter a blank message!</h4>";
        }

    }
    else {
        echo(
    "<h4>The message \"$newmsg\" already exists!</h4>");
        }

    fclose ($fd);


    //end of add new message process
    }
    ?>

    <BR><BR><Br><Br><Br>


            <form METHOD="GET" name="NewMessage" action="add.php">
    <?php

    echo"
        <input type='hidden' name='list' value='no'>
        <font size='6' face='Verdana, Arial, Helvetica, sans-serif'> Type a message in the box below and it'll be added to the signature.
        <br>
        <input type='text' name='newmsg' size='50'>
        <input type='submit' name='Submit' value='Submit'>
        </font> 
        </form>"
    ;
    ?>
    And the code that grabs a message:-

    PHP Code:
    $file "messages.txt"// Messages in here
    $fd fopen($file"r"); // Open the file for read only
    $words file($file);  // Put each word in an array
    fclose ($fd);
    $word_number rand(1,count($words)); // Show a random message on the image
    $word $words[$word_number-1]; // Position in array is $word_number-1
    $word ereg_replace("\n"""$word); 

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Using | to split words?

    it might be because you're removing the "new line" that you made ("\n"), and don't remove the carriage return ("\r"). you don't need to use a regular expression function to replace that, either (unless you're going to use a regular expression -- which you aren't now). use str_replace() instead.

    PHP Code:
    $word str_replace("\n"""str_replace("\r"""$word)); 

    edit: or, you can add a parameter to the file() call you already have using the flag "FILE_IGNORE_NEW_LINES". this will stop it from adding new lines to each entry in the array when building it -- and I'm thinking it might be a better thing to do than above (although if this doesn't work, you can try both).

    PHP Code:
    $words file("filename"FILE_IGNORE_NEW_LINES); 
    edit2: oh. and if you actually just want to remove using lines for saving your file altogether, you can build your file with a delimiter of "|" by replacing all of your line breaks with it. so, instead of all of the "\r\n" in the code that saves the file, change it to "|". then, when reading in the file, you could do something like this (slightly modified your code):
    PHP Code:
    $fd fopen($file"r"); // Open the file for read only
    $fc fread($fdfilesize($file)); //file contents
    fclose ($fd); 
    $words explode("|"$fc); //make an array
    $i rand(0count($words) - 1); //get a random index
    echo $words[$i]; 
    you could also use the example I just made to split your current file up. replace the first parameter of explode() with "\r\n" instead of "|", it might work better than file() for you, if you want to try to continue using line breaks instead of pipes.
    Last edited by kows; May 25th, 2007 at 09:23 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    190

    Re: Using | to split words?

    Thanks the first one worked fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width