Sorry for the huge post - big problem!
Hi guys. I'm trying to port a Python program to PHP which counts the number of words in a file - sounds simple? But it's not - it's counting a file in a programming language called RSL, which is to program robots. In this counting program, it needs to omit lines from counting where the line contains a certain command(s), and not count words after a comment indicator. Also, other characters count as words, but some don't.
The Python program is quite simple, but uses some annoyingly useful built in Pyton features that I really need, that in PHP is much more complex!
The idea is this:
1. A user writes a robot
2. He/she needs the number of words counted to see if it can be entered into a competition
3. He/she copy and pastes the robot into a form on a webpage
4. PHP counts the (correct) words and outputs it to the page
I don't know how to do this. I have tried, and failed. Here is the code which I was working on when I gave up:
<?php
$wordChars = '{}()[]+-*/%&^|=<>!\\';
$a_wordChars = array ( "{","}","(",")","[","]","+","-","*","/","%","&","^","|","=","<",">","!" );
$stopWords = array ('name', 'author', 'version', 'style');
$freeWords = array ('and', 'or', 'endif', 'endw');
$robot = $_POST['textarea'];
$code = explode("\n",$robot);
$linecnt = 0;
$t_wordcnt = 0;
$wordcnt = 0;
foreach($code as $line_num => $wline)
{
$line = trim($wline);
if(startsWith($line,"/") == 1 or startsWith($line,"#") == 1)
continue;
$t_wordcnt = str_word_count($line,0,$wordChars);
//echo($line . " - " . $t_wordcnt . "<br />");
}
function startsWith($line, $del)
{
$chars = str_split($line);
$f_char = $chars[0];
if($f_char == $del)
return 1;
else
return 0;
}
?>
<form class="countForm" action="index.php" method="POST">
<p>
<textarea name="textarea" cols="100" rows="30" wrap="off"></textarea>
</p>
<p>
<input type="submit" name="Count" value="Count" />
</p>
</form>
I did get a bit further, but I deleted it when it got too complicated!
IMPORTANT!:
Things it should and shouldn't count:
The $freeWords array is an array of words which shouldn't be counted.
The $stopWords array is an array of words which indicate the rest of the line shouldn't be counted.
Anything inside a "..." string (in the robot) should be counted as one word
The $wordChars array is an array of characters which count as words
I thinks thats everything.
Obviously I dont want to put anyone to the trouble of coding the whole thing, but some suggestions as to possible routes to solutions would be nice!
Just ask for more info!
Thanks in advance and sorry for the massive post,
Tom
EDIT: You can wee what it does at the moment at botcount.qubesite.co.uk