I have string value like : This is my first salary : $ 1000 How to remove entire character even $ symbol except 1000, i need to save 1000 into my table.
Regex would be what I would use.
preg_replace("/[^\d(,\.)?+\d]/", "", $input);
YAV (Yet Another Version)
<?php declare(strict_types=1);
$s = 'This is my first salary : $ 1000';
$s = preg_replace('/[^0-9]/','',$s);
echo $s;
But If the value is static then no need to add weight to the workout:
$s = 'This is my first salary : $ 1000';
$s = mb_substr($s, -4);
echo $s;
even so, strlen($s) - x;
You are placing a lot of emphasis on that being the case.
Example on a recent bug. Take the following
CTN-61420-4210234
{Location code}-{lot number}-{product number}
Business rules. The lot number needs to be extracted for later use to be sent to the database. Lot numbers will always be 5 digits long.
#Bug discovered: well sometimes the lot number will be 3 or 4 digits, but usually 5 digits.
The developer used string position rather than just splitting the string at the dash. Had they done that from the beginning, it wouldn’t have been a bug to begin with.
this brings back a lot of pain. we even had a lead who had to stand up in a meeting to get grilled because of a bug like this that came from parsing data “that will never change”, except when it does
is this directed at me?
i don’t see an emphasis at all in my post. Static means the digits never change (stay at five sans sometimes). But, whoa Nelly!, the code that you have posted and the purpose of the code is much different than mention by the OP. For one thing, preg_replace is silly in this example because a business should be focused on security, hence validation.
If i had to deal with a business model and a code such as
then i wouldn’t be implementing either preg_replace or subsrtring. I would be validating and extracting in the case of product ids of a business.:
$temp = explode("-", $productID);
if (count($temp) !== 3) {
return 'oops!';
}
list($loc, $lot, $prod) = $temp;
if (empty($loc) || empty($lot) || empty($prod)) {
return 'oops!';
}
then i would handle $lot which is all ready extracted without worry of digits involved
obviously, whitelisting $lot and verifying the code needs done but we are talking about extraction.
assert(is_int($lot) && strlen($lot) <= 5);
i certainly wouldn’t advise a business to stray from proper coding standards. We have enough of bad code like the unhashed passwords of Adobe, for example.
My point is merely if the OP knows for certain that the digits don’t change and are always at the last position. In reality, i would rather write hundreds of lines of security and verification codes than to take a chance of screwing up an entire business. Let’s not get ahead ourselves here. I’m no fool and i would not program a business app for disaster.
Morning John,
It was directed at you. Using substring, you are expecting the value to be the same size and in the same place all the time. So we are dealing with a string value, and it isn’t clear how it comes in, or from where. Predicating the decision on the length being 4 digits, what happens if the entered first salary is $10,000 or 120,000$? Now you have introduced bad data. preg_replace just ensures that what is coming in, does match the allowable format. doesn’t matter if they change the string to be,
It still gets the correct information every time.
Good morning to you, Sir, I hope that you are having a splendid start to your day
I am clearly assuming that the data remains the same. I don’t know if it changes because these details are lacking from the original post. So i’ve added a simple method for static data *failing to mention that it must remain static). I suppose that i assume a programmer retains control as i do. I examine my code to be certin that i get values that i seek, thus $ 1000 would be a variable in my program, which doesn’t need to be extracted from a string, rather appended.
echo 'This is my first salary : $' . $salary;
i don’t know where this data is coming from or why the salary is part of a string instead of a variable. More details are necessary but my post is solely assuming that the value is static.