HDFilmMaker2112 Posted June 8, 2012 Share Posted June 8, 2012 I'm looking for a way to convert a string into ascii number codes. I have the list of conversion words in an array, the problems lies in that each iteration through the array, is starting from the beginning of the array, and appending the next element in the array onto the end. i.e; the below is producing this: TEstABcDE12345678910 TEstABcDE12345678910TeshgaGDasf#1345 $string=array("TEstABcDE12345678910", "TeshgaGDasf#1345"); $asciiString=""; foreach($string as $string2){ for($i = 0; $i != strlen($string2); $i++) { $asciiString .= "&#".ord($string2[$i]).";"; } $asciiCode = str_replace("&", "&", $asciiString); echo $asciiString."<br />"; } How would I make it so that it only converts each array element individually. I also need to some how add a preg_match to this as well. The idea would be to have the array contain a list of "forbidden words" (javascript, alert, style, among others), and then to convert those forbidden words into their ASCII code equivalents. This is an attempt to go above and beyond htmlentities for XSS prevention. Quote Link to comment https://forums.phpfreaks.com/topic/263869-convert-string-to-ascii-code/ Share on other sites More sharing options...
scootstah Posted June 8, 2012 Share Posted June 8, 2012 I think I got it working with this: $words = array('TEstABcDE12345678910', 'TeshgaGDasf#1345'); $ascii = ''; foreach($words as $word) { $index = 0; while($index < strlen($word)) { $ascii .= ord($word[$index]); $index++; } echo $ascii . '<br />'; } However as an XSS prevention technique, I have my doubts. In my opinion, either use htmlentities() or htmlspecialchars() if you do not want to preserve HTML, or use HTML Purifier if you do want to preserve HTML. Quote Link to comment https://forums.phpfreaks.com/topic/263869-convert-string-to-ascii-code/#findComment-1352221 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 8, 2012 Author Share Posted June 8, 2012 That's unfortunately returning the same thing: TEstABcDE12345678910 TEstABcDE12345678910TeshgaGDasf#1345 <?php error_reporting(E_ALL); $words = array('TEstABcDE12345678910', 'TeshgaGDasf#1345'); $ascii = ''; foreach($words as $word) { $index = 0; while($index < strlen($word)) { $ascii .= "&#".ord($word[$index]).";"; $index++; } echo $ascii . '<br />'; } ?> As far as using htmlentities; I still plan too. This is just to go a step further and remove words that could be used in an attack. Quote Link to comment https://forums.phpfreaks.com/topic/263869-convert-string-to-ascii-code/#findComment-1352228 Share on other sites More sharing options...
scootstah Posted June 8, 2012 Share Posted June 8, 2012 Oh, my bad. I see the problem. You'll need to reset $asciiString for each iteration. So put $asciiString = ""; inside the foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/263869-convert-string-to-ascii-code/#findComment-1352233 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 8, 2012 Author Share Posted June 8, 2012 Perfect. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/263869-convert-string-to-ascii-code/#findComment-1352238 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.