JaredKnapp Posted October 11, 2008 Share Posted October 11, 2008 I'm stuck figuring out why my check for [space]BT[space] is failing in the code below. I have a really long string, and am running through it one character at a time. Each time I execute a loop iteration, I add the new character to the end of a 15 character array, and drop off the first character. You can see in the results that the [space]BT[sPACE] is running though my array, but my IF statement in _checkToken(...) fails to notice it. Does anyone have any ideas? (where am I being stupid?): var $numberOfCharsToKeep = 15; <<START LOOP THROUGH LONG STRING>> // Store the recent characters for // when we have to go back for a checking echo "Adding Character '".$c."' to ".implode('|',$previousCharacters)."<br>"; for ($j = 0; $j < $this->numberOfCharsToKeep - 1; $j++) { $previousCharacters[$j] = $previousCharacters[$j + 1]; } $previousCharacters[$this->numberOfCharsToKeep - 1] = $c; echo "NEW Previous Characters=".implode('|',$previousCharacters).'<br>'; // Start of a text object if (!$inTextObject && $this->_checkToken(array("BT"), $previousCharacters)) { echo "DEBUG: INTEXT=TRUE<br><br>"; $inTextObject = true; } <<END LOOP THROUGH LONG STRING>> // <summary> // Check if a certain 2 character token just came along (e.g. BT) // </summary> // <param name="tokens">the searched token</param> // <param name="recent">the recent character array</param> // <returns></returns> function _checkToken($tokens, $recent) { foreach($tokens as $token) { echo "token[0]=".$token[0]." token[1]=".$token[1]."<br>"; echo "recent-4='".$recent[$this->numberOfCharsToKeep - 4]."' recent-3='".$recent[$this->numberOfCharsToKeep - 3]."' recent-2='".$recent[$this->numberOfCharsToKeep - 2]."' recent-1='".$recent[$this->numberOfCharsToKeep - 1]."'<br>"; if ( ($recent[$this->numberOfCharsToKeep - 3] == $token[0]) && ($recent[$this->numberOfCharsToKeep - 2] == $token[1]) && (($recent[$this->numberOfCharsToKeep - 1] == ' ') || ($recent[$this->numberOfCharsToKeep - 1] == 0x0d) || ($recent[$this->numberOfCharsToKeep - 1] == 0x0a)) && (($recent[$this->numberOfCharsToKeep - 4] == ' ') || ($recent[$this->numberOfCharsToKeep - 4] == 0x0d) || ($recent[$this->numberOfCharsToKeep - 4] == 0x0a)) ) { echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RETURNING TRUE<br><br>"; return true; } } return false; } Adding Character ' ' to |/|i|m|g|0| | |D|o| |Q| |B|T NEW Previous Characters=/|i|m|g|0| | |D|o| |Q| |B|T| token[0]=B token[1]=T recent-4=' ' recent-3='B' recent-2='T' recent-1=' ' 1Adding Character '1' to /|i|m|g|0| | |D|o| |Q| |B|T| NEW Previous Characters=i|m|g|0| | |D|o| |Q| |B|T| |1 token[0]=B token[1]=T recent-4='B' recent-3='T' recent-2=' ' recent-1='1' Adding Character ' ' to i|m|g|0| | |D|o| |Q| |B|T| |1 NEW Previous Characters=m|g|0| | |D|o| |Q| |B|T| |1| token[0]=B token[1]=T recent-4='T' recent-3=' ' recent-2='1' recent-1=' ' 0Adding Character '0' to m|g|0| | |D|o| |Q| |B|T| |1| Link to comment https://forums.phpfreaks.com/topic/127989-help-parsing-strings-arrays/ Share on other sites More sharing options...
CroNiX Posted October 11, 2008 Share Posted October 11, 2008 Please post your code within code tags. Its the '#' icon in the editor and start your code with <?php so it will get formatted and colorized properly. Link to comment https://forums.phpfreaks.com/topic/127989-help-parsing-strings-arrays/#findComment-662748 Share on other sites More sharing options...
JaredKnapp Posted October 11, 2008 Author Share Posted October 11, 2008 Sorry, I don't see where to edit my original post, so here it is properly formatted: MEMBER CONSTANT <?php var $numberOfCharsToKeep = 15; START LOOP THROUGH LONG STRING: <?php // Store the recent characters for // when we have to go back for a checking echo "Adding Character '".$c."' to ".implode('|',$previousCharacters)."<br>"; for ($j = 0; $j < $this->numberOfCharsToKeep - 1; $j++) { $previousCharacters[$j] = $previousCharacters[$j + 1]; } $previousCharacters[$this->numberOfCharsToKeep - 1] = $c; echo "NEW Previous Characters=".implode('|',$previousCharacters).'<br>'; // Start of a text object if (!$inTextObject && $this->_checkToken(array("BT"), $previousCharacters)) { echo "DEBUG: INTEXT=TRUE<br><br>"; $inTextObject = true; } END LOOP THROUGH LONG STRING TOKEN CHECKING: <?php // <summary> // Check if a certain 2 character token just came along (e.g. BT) // </summary> // <param name="tokens">the searched token</param> // <param name="recent">the recent character array</param> // <returns></returns> function _checkToken($tokens, $recent) { foreach($tokens as $token) { echo "token[0]=".$token[0]." token[1]=".$token[1]."<br>"; echo "recent-4='".$recent[$this->numberOfCharsToKeep - 4]."' recent-3='".$recent[$this->numberOfCharsToKeep - 3]."' recent-2='".$recent[$this->numberOfCharsToKeep - 2]."' recent-1='".$recent[$this->numberOfCharsToKeep - 1]."'<br>"; if ( ($recent[$this->numberOfCharsToKeep - 3] == $token[0]) && ($recent[$this->numberOfCharsToKeep - 2] == $token[1]) && (($recent[$this->numberOfCharsToKeep - 1] == ' ') || ($recent[$this->numberOfCharsToKeep - 1] == 0x0d) || ($recent[$this->numberOfCharsToKeep - 1] == 0x0a)) && (($recent[$this->numberOfCharsToKeep - 4] == ' ') || ($recent[$this->numberOfCharsToKeep - 4] == 0x0d) || ($recent[$this->numberOfCharsToKeep - 4] == 0x0a)) ) { echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RETURNING TRUE<br><br>"; return true; } } return false; } RESULTS: ====================================================== Adding Character ' ' to |/|i|m|g|0| | |D|o| |Q| |B|T NEW Previous Characters=/|i|m|g|0| | |D|o| |Q| |B|T| token[0]=B token[1]=T recent-4=' ' recent-3='B' recent-2='T' recent-1=' ' 1Adding Character '1' to /|i|m|g|0| | |D|o| |Q| |B|T| NEW Previous Characters=i|m|g|0| | |D|o| |Q| |B|T| |1 token[0]=B token[1]=T recent-4='B' recent-3='T' recent-2=' ' recent-1='1' Adding Character ' ' to i|m|g|0| | |D|o| |Q| |B|T| |1 NEW Previous Characters=m|g|0| | |D|o| |Q| |B|T| |1| token[0]=B token[1]=T recent-4='T' recent-3=' ' recent-2='1' recent-1=' ' 0Adding Character '0' to m|g|0| | |D|o| |Q| |B|T| |1| Link to comment https://forums.phpfreaks.com/topic/127989-help-parsing-strings-arrays/#findComment-662777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.