koopkoop Posted December 9, 2008 Share Posted December 9, 2008 Is there a built in function to filter a string based on a coder's input ascii table value? Eg. Filter everything above 39 in the Ascii table I've found the FILTER_FLAG_STRIP_HIGH (that kills all above 127), but I need more control over the ascii range that gets filtered. Link to comment https://forums.phpfreaks.com/topic/136178-filter-based-on-ascii-table-values/ Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 There might be a more efficient way, but you could just do <?php $str = 'this is a test%#$'; $newstr = ''; for($i = 0; $i < strlen($str); ++$i) { if(ord($str[$i]) <= 39) $newstr .= $str[$i]; } echo $newstr; // "%#$" ?> of course, I didn't test this, but you get the idea edit: but it might just be more efficient to use another method, what do you need this for? another edit: meant to use ord() not chr().... darn memory is going out on me Link to comment https://forums.phpfreaks.com/topic/136178-filter-based-on-ascii-table-values/#findComment-710361 Share on other sites More sharing options...
koopkoop Posted December 9, 2008 Author Share Posted December 9, 2008 There might be a more efficient way, but you could just do <?php $str = 'this is a test%#$'; $newstr = ''; for($i = 0; $i < strlen($str); ++$i) { if(ord($str[$i]) <= 39) $newstr .= $str[$i]; } echo $newstr; // "%#$" ?> of course, I didn't test this, but you get the idea edit: but it might just be more efficient to use another method, what do you need this for? another edit: meant to use ord() not chr().... darn memory is going out on me I have a csv data file with products descriptions from the product manufacturer. Unfortunately, the manufacturer can never seem to get their exports right so, alot of times, there'll be random products with descriptions that have garbage characters inserted into it (Eg square box character or A with a carat above it character). I want to write a filter to take in these description strings and filter out(or replace) all these garbage characters. Link to comment https://forums.phpfreaks.com/topic/136178-filter-based-on-ascii-table-values/#findComment-710770 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 Then it might be more efficient to use regex as looping through all characters in a string with php is fairly expensive. It's up to you how many characters you wish to allow. Link to comment https://forums.phpfreaks.com/topic/136178-filter-based-on-ascii-table-values/#findComment-710774 Share on other sites More sharing options...
koopkoop Posted December 9, 2008 Author Share Posted December 9, 2008 Then it might be more efficient to use regex as looping through all characters in a string with php is fairly expensive. It's up to you how many characters you wish to allow. How do i specify regex values in terms of their ascii table equivalent? I'm totally new to advanced regex concepts. Link to comment https://forums.phpfreaks.com/topic/136178-filter-based-on-ascii-table-values/#findComment-710818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.