PHP - Figuring Out Censor.php
So I have been reviewing my codebase that i stepped away from for a couple of years. Surprisingly (or not), so far I understand almost all of my code. But I have a function that **** out dirty words, and there is one section of the code that isn't making sense. (I added var_dumps, but I'm just not seeing what it is doing.) Below is slightly simplified code - since my real code uses the database plus a bunch of words I can't post here! I have added comments where I'm hung up... <?php // Initialize Variables. $patternsArray = array(); $replacementsArray = array(); $tempArray = array(); $matchesArray = array(); // Un-censored text. $text = "Cassie, slipped on some glass and cut her ass!<br> 'Goshdamnit! Who left this damn mess out?' she cried.<br> 'Well, SHOOT, you should be more careful!' Mary laughed.<br> 'Hello! Why don't you clean up after yourself!' Cassie screamed.<br> 'Go to HELL!' Mary yelled back.<br> Then suddenly, blood started shooting everywhere."; // Create Replacements. $replacementsArray[0] = '***'; // ass $replacementsArray[1] = '****'; // damn $replacementsArray[2] = '****'; // shoot $replacementsArray[3] = '****'; // hell // Create Patterns. $patternsArray[0] = "/\b(ass)(s?)\b/i"; // Word pattern $patternsArray[1] = "/damn/i"; // Substring pattern $patternsArray[2] = "/\b(shoot)(s?)\b/i"; // Word pattern $patternsArray[3] = "/\b(hell)(s?)\b/i"; // Word pattern // Sort Arrays. // ksort($patternsArray); // ksort($replacementsArray); // What does this do???? foreach ($patternsArray as $pattern){ preg_match_all($pattern, $text, $tempArray, PREG_PATTERN_ORDER); // (1) // preg_match_all($pattern, $text, $matchesArray, PREG_PATTERN_ORDER); // (2) $matchesArray = array_merge($matchesArray, $tempArray[0]); // (3) /* If I comment (1) and (3) and uncomment (2), * then var_dump($matchesArray) displays nothing. * Not realy sure what this block of code does... */ } // Clean text. $cleanText = preg_replace($patternsArray, $replacementsArray, $text, $limit=-1, $allCount); ksort($matchesArray); echo "<b>DIRTY TEXT:</b><br> $text"; var_dump($patternsArray); var_dump($replacementsArray); var_dump($tempArray); var_dump($matchesArray); echo "<b>CLEAN TEXT:</b><br> $cleanText"; ?>
In the test I run locally - again I couldn't really post that here - my function seems to work, but I don't see how the parts i commented above work/help get the end results I want. The goal of reading all of my code line-by-line is to UNDERSTAND it, so that when I go to code my final module, I know my head is in the game. Thanks. Edited February 6 by SaranacLake Similar Tutorialsat this point, I've authenticated with the login service, and I believe I'm now passing the cookie correctly to the ADBAccess service, but when I do a soapcall to getCMSData, I'm getting an exception. Here's the relevant code: $agDataService->__setCookie('cookie', $s_Cookie); $a_includeList[0]="*"; $a_getCMSTables_args['opName']='accounts'; $a_getCMSTables_args['startDate']='01/07/2011'; $a_getCMSTables_args['endDate']='01/08/2011'; $a_getCMSTables_args['lastID']=''; $a_getCMSTables_args['includeList']= a_includeList; $agTables = $agDataService->__soapCall('getCMSData', $a_getCMSTables_args, null, new SoapHeader('http://webservice.agemni.com', 'cookie', $s_Cookie)); and here's the exception: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object. at ADBAccess.setErrorXML(XmlDocument& responseDoc, XmlElement& DocRoot, String ErrorMessage, Int32 ErrorNum, Int32 numRecords) at ADBAccess.getCMSData(String opName, String startDate, String endDate, String lastID, String[] includeList) --- End of inner exception stack trace --- any ideas? This PHP program has been haunting IT around the world, see if you can correct it? Its open source code!
Errors:
Notice: Uninitialized string offset: 0 in C:\wamp\www\language_files\language.inc.php on line 33
Notice: Undefined offset: 2 in C:\wamp\www\language_files\language.inc.php on line 50
Code:
<?php
$strLangShort = $_REQUEST['language']; if ($strLangShort == '') { $strLangShort = $_REQUEST['strLanguage']; } $strFilename = 'gui_'.$strLangShort.'.langprop'; $strLanguagePath = "../language_files/".$strFilename; $pFile = @fopen($strLanguagePath, "r"); if ($pFile == null) { $strLanguagePath = "language_files/".$strFilename; $pFile = @fopen($strLanguagePath, 'r'); if ($pFile == null) { $strLanguagePath = "../../language_files/".$strFilename; $pFile = @fopen($strLanguagePath, 'r'); } } $arrTranslation; if (null != $pFile) { while (!feof ($pFile)) { $strLine = trim(fgets($pFile, 4096)); if ( ($strLine[0] != "#") && (strlen($strLine) > 0) && (substr($strLine,0,5)!="_jotl")) { $nPos = strpos($strLine, "="); $strId = substr($strLine, 0, $nPos); $strRest = trim(substr($strLine, $nPos+1)); $arrInfos = explode("@@@", $strRest); $strValue = $arrInfos[0]; $arrTranslation[$strId] = $strValue; } else { $nPos = strpos($strLine, "="); $strConfig = substr($strLine, 0, $nPos); $splitConfig = explode (".", $strConfig); if ($splitConfig[2] == 'encoding') { $DEFAULT_CHARSET = trim(substr($strLine, $nPos+1)); } } } fclose($pFile); } $CIRCULATION_MNGT_ADDCIRCULATION; foreach($arrTranslation as $key => $value) { $$key = $value; } function escapeSingle($string) { return str_replace("'", "\\'", $string); } function escapeDouble($string) { return str_replace("\"", "\\\"", $string); } ?> I know this is stupid newbie stuff, but I can't think about this the right way. And this question might very well make NO sense. If so, I apologize in advance. I'm trying to consolidate code. I basically have lots of quizzes, all with their own answers, which I compare using AJAX. I had individual pages for each quizzes answers to do that comparison and everything was working fine. It would do the following... Code: [Select] <?php $userinput = strval($_GET['userinput']); $userinput= trim($userinput); $userinput = strtolower($userinput); switch ($userinput) { case 'heat': case 'miami heat': echo 'Miami Heat'; exit; case 'magic': case 'orlando magic': echo 'Orlando Magic'; exit; case 'jazz': case 'utah jazz': echo 'Utah Jazz'; exit; default: echo 'incorrectguess'; } ?> But now I am storing the answers in a MySQL database (for example, one answer set is in the database as "Miami Heat/heat/miami"...each allowed answer is separated by a "/", with the first answer being the "main" correct answer). So now, I want to just have ONE "comparison" page and populate the "cases" into that page dynamically, from the database. This is what I came up with so far, but it's not working... Code: [Select] <?php $userinput = strval($_GET['userinput']); $userinput= trim($userinput); $userinput = strtolower($userinput); if ($_GET['quizid'] == "") { redirect_to('index.php'); } else { $quizid=$_GET['quizid']; } $sql = "SELECT * FROM answers WHERE quizid = $quizid"; $result = mysql_query($sql, $connection); if (!$result) { die("Database query failed: " . mysql_error()); } else { switch ($userinput) { while ($results = mysql_fetch_array($result)) { $answers = explode("/", $results['answer']); $n = count($answers); for ($i=1; $i < $n; $i++) { case '$answers[$i]': } echo '$answers[0]'; exit; default: echo 'incorrectguess'; } } ?> What I'm having trouble grasping is where I need to use "echo" or NOT use "echo". For example, where I have "switch ($userinput) {" in the NEW code, does that need to actually be "echo 'switch ($userinput) {';"? I'm trying different combos and failing, so just curious if someone can help me understand what, if anything, needs to be echoed when doing it the 2nd way (dynamic info) rather than the 1st way (static info) |