Jump to content

Create PHP code which convert words and save into file - need help


mladja04

Recommended Posts

Hello,

 

I am novice in PHP, I read book and try to write some PHP code, but I don't have success in that my attempt.

Do someone can to help me? I think that this what I work isn't hard, but I don't know to do.

 

 

I need to create in PHP some code which will:

 

1. In text field I enter some words in 2-3 rows, example

word11 word12 word13

word21 word22 word23

word31 word32 word33

and click to button Convert,

 

2. After click on that button php script will take that words and convert into this mode:

word11-word12-word13(TAB here without space)word11 word12 word23

word21-word22-word23(TAB here without space)word21 word22 word23

word31-word32-word33(TAB here without space)word31 word32 word33

 

(spaces between words will convert into - and between first and second mode with TAB)

 

3. On that way converted text to be saved into new file which will be with name of first line "word11 word12 word13".

 

 

 

I know procedure to make this, but I don't know syntax very well :(

 

Can someone write me code or part of code, to give me idea how I to make this?

 

 

Thank you very much, Mladen

Link to comment
Share on other sites

this is very basic, if you are taking user input you should probably add some field validation but here you go:

<?php
$strOutPut = ""; 
if(isset($_POST['txtInput']) && trim($_POST['txtInput']) != "") 
{
    $arrLines = explode("\n", str_replace("\r", "", $_POST['txtInput']));
    array_walk($arrLines, "trim");

    foreach($arrLines as &$value)
    {   
        $strOutPut .= str_replace(" ", "-", $value) . "\t" . $value . "\n";
    }   

    $strOutPut = substr($strOutPut, 0, -1);
    
    $fp = fopen('path/to/file.txt', 'w');
    fwrite($fp, $strOutPut);
    fclose($fp);
}
?>
<html>
    <head>
    </head>
    <body>
        <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
            <textarea name="txtInput" style="width:500px; height:300px;"><?php echo trim($strOutPut) != ""?$strOutPut:""; ?></textarea><br />
            <input type="submit" name="btnSubmit" value="submit" />
        </form>
    </body>
</html>

Link to comment
Share on other sites

Vauuu, this is great and work, thank you very, very, very much.

 

I guess that I can to add new field in html in which I can to enter name of file and script to create file with that name, not I to create it before code execute. Do it is possible that script create file and to give name - take first words from text field (or first row) and after that to put converted words?

 

Thank you very very much, Mladen

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.