Jump to content

Filter based on Ascii table values


koopkoop

Recommended Posts

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

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 :)

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.