How to twist the words of others to my own dire purposes?
November 29, 2006 2:21 AM   Subscribe

I want to create a web application in which the user enters text into a field and clicks on a submit button, and the text is then altered based on a set of conditions and given back to the user. What's the easiest way of doing this? I have a vague idea that I should use Perl and CGI. I don't know Perl or CGI, but I do know a bit of Java, a bit about servlets and JSPs, some Visual Basic, and quite a bit about http.

I realize that there is a certain irony to my posting this on metafilter, where there is clearly something similar at work, but that unfortunately, being on the user end of that doesn't make it clear to me what the actual mechanism is.
posted by bingo to Computers & Internet (13 answers total)
 
There are hundreds of ways of doing it. Do you want to do it server side or client side? How complex is the alteration?

If the alteration is tricky, I can't help much, I suck at playing with text.

If it is simple, I would use PHP or Python. PHP is easier to get started with, is very forgiving (allowing very crappy code to 'work'), and works fine for simple stuff like this. Most hosting providers have it for free.

Client side, I would do it with Flash (I write actionscript for a living, would take the least time for me to write), and can figure out how to do it with javascript (I am a bit rusty, and was never very advanced).

If you give a more beefed up spec, I can help you with some code samples, anything to keep me distracted from my job :)
posted by Dataphage at 2:40 AM on November 29, 2006


Response by poster: It doesn't really matter to me whether it's server or client side, although instinctively I want it to be server-side, as I think I'd prefer to hide the bulk of the mechanism from the client (though this isn't mandatory). We're talking about pretty simple stuff here...or at least, I imagine that it's simple. Kind of an advanced 'search and replace' sort of thing...that is, just advanced enough that you can't do it with a product like Word, because you need regular expressions (or at least something more complex than * as a wildcard), and because there are a number of different combinations that need to be checked for. I have done similar things in VBA, but a) I'm pretty sure I was doing it in a horribly inefficient way, b) I don't even remember how to do that anymore anyway, and c) the project I have in mind would be useful to a lot of other people, so I want to put it online.

I was reading a bit about PHP, and that may be the thing...thanks for the offer to help, I may email you later after I do a bit more reading.
posted by bingo at 3:00 AM on November 29, 2006


There's a million ways to do this. If you haven't done much programming before, I'd recommend using Python (a programming language) with Cherrypy (a web server that uses Python).
  1. Install Python
  2. Install Cherrypy (download, extract, and run python setup.py install)
  3. Create a file named mysite.py with contents similar to the following
    import cherrypy
    
    class MySite:
      
      def index(self):
        return file('mypage.html').read()
    
      def submitpage(self, myfield='):
        if myfield.startswith('foo'):
          return myfield.upper()
        else:
          return myfield.replace('a', 'b')
    
      index.exposed = True
      submitpage.exposed = True
    
    cherrypy.quickstart(MySite())
    

  4. Create a file named mypage.html that contains the html for the web page, including a form with action "submitpage" and that has a field named "myfield".

  5. From the command-line, run python mysite.py

Then, run through the official Python tutorial and modify this to suit your needs.
posted by gsteff at 3:09 AM on November 29, 2006


Oops... forgot to provide download pointers. Python and CherryPy.
posted by gsteff at 3:11 AM on November 29, 2006


Yikes, typo on the "submitpage" line of the code above... there's a missing quotemark. The line should be:

def submitpage(self, myfield='):
posted by gsteff at 3:14 AM on November 29, 2006


Response by poster: Maybe I should mention that I have a macbook...only since it comes with various resources installed...including a web server, right? Thanks for the answers so far. I'm skimming a PHP tutorial and will look into Python as well...
posted by bingo at 3:18 AM on November 29, 2006


WTF...

def submitpage(self, myfield=""):
posted by gsteff at 3:20 AM on November 29, 2006


I would say absolutely definitely do it in PHP. The setup overhead of something like Cherrypy is way higher. It's also got kind of a weird metaphor going on (the exposed thing? quickstart? hmm...) that will take some getting used to. PHP is simpler, and I bet you'll have better luck with it. It's also used by tons of people, so there's a lot more written about it on the web if you get stuck.

Your macbook will happily run either PHP and Python, so that's not really an issue.

Good luck!
posted by heresiarch at 5:35 AM on November 29, 2006


OK, if he uses php, he's going to need to use the perl regexes anyways for this, no? Is php ever the correct answer for data manipulation?

It's not like perl isn't enormously popular too. maybe this?
posted by shownomercy at 6:06 AM on November 29, 2006


Best answer: PHP is simple. not so pretty, but simple. and PHP can do text munging fine. (the preg_* methods mean perl regular expressions.)

save this code as "test.php" and access it through a webserver. (it converts vowels to "x".)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>test</title>
</head>
<body>

<?php

$text = $_GET[text];
if (!empty($text)) {
$text = preg_replace("/[aeiou]/", "x", $text);
print "<h2>Result</h2>\n" . "<P>" . $text . "</P>";
}

?>

<form action="test.php" method="get" accept-charset="utf-8">
enter something: <input type="text" name="text" value="<?php $_GET[text] ?>">

<p><input type="submit" value="Continue..."></p>
</form>
</body>

posted by kamelhoecker at 8:08 AM on November 29, 2006


btw, on your macbook, you can probably save it as:

/Library/WebServer/Documents/test.php

and access it through:

http://127.0.0.1/test.php
posted by kamelhoecker at 8:13 AM on November 29, 2006


If there's any sort of database access involved, make sure to clean the text against SQL Injection, and if there are any cookies involved, clean the text against Cross-site scripting
posted by nomad at 9:28 AM on November 29, 2006


It doesn't really matter what language you use since the point of the whole thing is the transformation. Assuming you already know the rules by which the text will be transformed, I see two main choices: a language you're comfortable with and a language best suited for implementing your transformation algorithm. That said, the Java you already know is not the greatest text-manipulation language, Perl is great with text, and PHP is somewhere in the Perl side of the middle. I don't think VB or JSP will help you much here except for the techniques that help in general programming habits.
posted by rhizome at 11:41 AM on November 29, 2006


« Older shouting 'damn kids' whilst shaking fist   |   Should I include an incomplete undergraduate... Newer »
This thread is closed to new comments.