Jump to content

tomash

New Members
  • Posts

    5
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

tomash's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Sorry ignore that, I got a message! But any help?
  2. Just wondering, why has this been moved? It's not about the Python - it's incidental that the original is in Python. I just want to know how to do it in PHP? I don't mind, I just think it's odd.
  3. Sure: def BotWords (code, verbose = False, wordChars = '{}()[]+-*/%&^|=<>!\xaa\xa5\xa4\xa3\x7c\x26', stopWords = ['name', 'author', 'version', 'style'], freeWords = ['and', 'or', 'endif', 'endw']): if type (code) == type (' '): botFile = open (code, 'r') code = botFile.readlines () botFile.close () wordTbl = string.maketrans (wordChars, ' ' * len (wordChars)) wordCnt = 0 for line in code: line = line.split ('#')[0] line = line.split ('//')[0] line = line.translate (wordTbl) line = line.split () lineCnt = 0 inString = 0 finalWords = [] for word in line: if word.lower () in stopWords: break elif word.endswith ('"'): lineCnt += 1 inString = 0 finalWords.append (word) elif word.startswith ('"'): inString = 1 elif not inString and word and word.lower () not in freeWords: lineCnt += 1 finalWords.append (word) wordCnt += lineCnt if verbose and finalWords: print (lineCnt, finalWords) return wordCnt
  4. 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
  5. [code](Line 56) $updt = "UPDATE HillHouse SET content='$_POST['pCContent']', date='$_POST['date']', time='$_POST['time']' WHERE name='$pagename'";[/code] the error I'm getting is: [quote]Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in ******************************** on line 56[/quote] It is connecting to the database fine. I think this may be in the wrong place - sorry if it is. Its just it uses php aswell so... Thanks for your help. It's probably something stupid i've missed!
×
×
  • 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.