How to use chr() function in an array for cleaning strings from dangereous characters in PHP?

40
February 11, 2019, at 4:30 PM

Hi everyone i was working on some php forms. I am a newbie so i might used a really annoying way to solve my problem. Here is my try:

<?php
function ascii(){
    $j = 0;
    for ($i = 33; $i < 65; $i++){
        // turn $i to ascii
        $k = chr($i);
        // put it in an array
        $text[$j] = $k;
        $j++;
    }      
    print_r($text);  
}
?>
Output for the ascii() :  Array ( [0] => ! [1] => " [2] => # [3] => $ [4] => % [5] => & [6] => ' 
[7] => ( [8] => ) [9] => * [10] => + [11] => , [12] => - 
[13] => . [14] => / [15] => 0 
[16] => 1 [17] => 2 [18] => 3 [19] => 4 
[20] => 5 [21] => 6 [22] => 7 [23] => 8 
[24] => 9 [25] => : [26] => ; [27] => < 
[28] => = [29] => > [30] => ? [31] => @ ) 

Even if it is a wrong way to collect wanted ascii characters in an array please just say it. Also i think i made a mistake with function and arrays but i couldn't solve it. What should i pass to function and return from it. If i use this function in something , should i pass the arguments by reference?

UPDATE

I have an output now. Time to improve function :). I am planning to use this array(after change something) with trim function in another loop. Is that a proper way to clean ASCII characters from a string?

Answer 1

If you want to use chr I would recommend to change your function to:

<?php
function ascii(){
    for ($i = 33; $i < 65; $i++)
        $text[] = chr($i);
    return $text;
}
print_r(ascii());

If you want to filter non-alphabetic char you may use ctype-alpha.

If you had different propose for the asscii array please share you case clearly.

Answer 2

I 'm not understood how you will use that array, but in any case you can fill it just by

range('!', '@');
READ ALSO
Show concrete data of array with objects [duplicate]

Show concrete data of array with objects [duplicate]

This question already has an answer here:

43
No styling when converting DOCX into PDF

No styling when converting DOCX into PDF

I am trying to convert a DOCX file to PDF with PHPWordWhen I execute the script it looks like that some style elements are not converted

57
Connection Failed: php_network_getaddresses: getaddrinfo failed: No such host is known

Connection Failed: php_network_getaddresses: getaddrinfo failed: No such host is known

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is knownin C:\xampp\htdocs\Hostel-Management-System-master\includes\config

77
How to differentiate data to be read between two tables when username and password is passed

How to differentiate data to be read between two tables when username and password is passed

I am using volley in my mobile development login and instead of one table, I used two

50