P
P
Panzers2020-05-24 21:40:16
PHP
Panzers, 2020-05-24 21:40:16

What's the easiest way to write stripping spaces, tags, and converting to lowercase?

Hello!
There is a code:

$pin = strtolower($_POST['crankpin-1'].$_POST['crankpin-2'].$_POST['crankpin-3'].$_POST['crankpin-4']);

$arr = str_replace('/\s/','',str_split($pin));

echo '<pre>';
print_r($arr);
echo '</pre>';

There are several questions:
1) It seems to me that something is wrong here (it's cumbersome or something ..), it certainly works, but maybe there are easier options?
2) In the form 4 text inputs, each limited to 4 characters. Accordingly, 16 characters in total come to the handler. Next, you need to remove the spaces, convert everything to lower case, and split it into an array. Everything seems to be good, but if we remove the spaces, then the output array already has 17 characters. wtf? Because further along the code it will be necessary to pull out data from the xml file, and the array is already missing for selection :(
3) What is the best way to filter the data from the form by post? In terms of tags and other superfluous things, there are only letters and numbers and everything is needed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2020-05-24
@ThunderCat

firstly, the cryptic string c $arr = str_replace('/\s/','',str_split($pin));will work very ambiguously, if only because str_split() does not support multibyte encodings. Secondly, of course, it is not needed there, since str_replace () works with strings in the third argument, but it does not work with regular expressions in the first, so this line does not work for you at all. Otherwise, everything is quite in the best traditions of delusion generators.
Briefly: If you have fields of certain types, check them separately for matching the required types, there are filters and helpers for this. Saving on letters was relevant in the 17th century, when paper was expensive.

M
marmuff, 2020-05-24
@marmuff

By registers - there are already implemented functions in the puff for converting strings to lower case and UPPER CASE.
To remove spaces and tags - regular expressions are at your service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question