Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Helllo, I wonder if there is a way to compare two strings and get the number of different letters (or any other metrics of difference). strcmp() doesn't really work, since it return some random numbers, which I can't use. My goal is to compare two strings and find if they are different in more than 5 symbols. Can someone give me a hint. Thank you for your time.
Sounds like one of the rare occasions where levenshtein() can be used.
The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform str1 into str2.
You could try using PHP's similar_text function:
$matching_char_count = similar_text($var_1, $var_2, $percent);
echo $matching_char_count;
echo $percent;
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Not terribly familiar with regex, but I'm guessing it's going to make life much easier for my current need.
I need to validate that a string contains the correct sequence of numbers and letters, so that it always follows the format "AA99999A", where A can be any A-Z character and 9 can be any 0-9 character. Other than exploding the string and validating the characters individually, how would the best way to handle this be?
The result can be a simple true / false as I don't need to specify which characters are incorrect
How about:
preg_match('/^[A-Z]{2}\d{5}[A-Z]$/', $string);
You can try this regex:
'/^[A-Z]{2}\d{5}[A-Z]$/i'
PHP's preg_match will do the trick:
$string_to_be_validated = "AB12345C";
if (preg_match("/\A[A-Z]{2}[0-9]{5}[A-Z]{1}\z/", $string_to_be_validated)) echo "valid";
else echo "invalid";
Here you can find more information on this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create two variables from one string in PHP.
e.g.
$string = "2,50";
to
$a = "2";
$b = "50";
How to do that?
I want to use it to <sup> the cents in the price and style it differently, but I have this number as a whole. So I need to devide it into two pireces.
the format of the string is always with two decimal points and a comma as a delimiter, os this should be a little help.
Any idea how to do this using PHP?
IMPORTANT! I need to do that without using arrays in PHP!
You can use the list construct along with explode() to achieve this:
list($a, $b) = explode(',', trim($string));
Demo.
Use preg_split() this will give you an array with two entries, add the values of those entries to your original variables.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to make a number validation that checks if the user hasn't already added the points after each three digits. I plan to do this verification using refex
So for example 11.231.121.313 is a valid number, also 11231121313 but 11231.121.313 is not.
^(\d+|\d{1,3}(\.\d{1,3})*)$
The first alternation allows you to have simply all digits. The second checks for 1-3 digits optionally followed by groups of a decimal point with 1-3 following digits. This works for your examples.
try this
if (preg_match('/^(\d{1,3}(\.\d{3})+|\d+)$/', $number)) {
// correct number
}
UPD: Add expression for only numbers
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to retrieve a list of words, but don't want any words that contain a number or numbers in them.
The idea is very simple, but I'm not even sure how to try. I have one column of words some of which have numbers in them. I only want to retrieve the ones without numbers.
How would I do this in my SQL query?
Thanks
Use a regular expression, either with php's preg_match, or with REGEXP in mysql.
SELECT list_col FROM list_table WHERE list_col NOT REGEXP '[0-9]'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking to floor/round a number with a certain significance.
For example 2499 => 2400. This can be achieved in excel using =FLOOR(2499, 100)
What is the equivalent function for this in PHP? Or is there a library / custom function that solves this problem?
Use this approach:
https://stackoverflow.com/a/7491541/2731161
So:
round_down('2499',-2); #results in 2400
Note the precision value. You'll need the round_down function from the posted answer.
it's floor();
(would you believe?) there's no precision setting.. this function is from the PHP manual page - purports to add decimal places though I've not tested it.
Otherwise you could multiply by 100 (if you want 2 dps), use floor(), then divide by 100 to get the result.
Floor decimal numbers with precision:
function floor_dec($number,$precision,$separator){
$numberpart=explode($separator,$number);
$numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);
if($numberpart[0]>=0)
{$numberpart[1]=floor($numberpart[1]);}
else
{$numberpart[1]=ceil($numberpart[1]);}
$ceil_number= array($numberpart[0],$numberpart[1]);
return implode($separator,$ceil_number);
}
echo floor_dec(1.125,2,"."); //1.12
echo floor_dec(-1.3436,3,"."); //-1.344
echo floor_dec(102938.1,4,"."); //102938.1