This question already has answers here:
Character-wise string diff in PHP
(4 answers)
Closed 9 years ago.
Is it possible to return the number of characters which differ between two strings?
In my case, I would like to run a check to quantify how much a user's draft has changed vs the database version. With this information, the system can decide weather or not the draft needs to be auto-saved.
For example:
echo string_diff("Hello world", "Hi World");
This should output "5", indicating that characters "ello" & "W" are different.
Is this possible with PHP? How about with mySQL?
On the PHP side your options are
similar_text which calculates the percent similarity between two strings
levenshtein - which calculates the edit distance between two strings (the number of key strokes it takes to transform string a into string b)
on mysql you can use this user defined function to calculate the levenshtein distance
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
I'm currently working on retrieving data from coinmarketcap API and I would like to limit the strings to 5. I retrieve the data and then multiply the value *1.07 and the result to MXN, but the string is long, like 25.65675734343, I want to limit that string, this is my echo:
echo $xrpprice*$rate*$fxrates['rates']['MXN'];
Use round:
$result = $xrpprice*$rate*$fxrates['rates']['MXN'];
echo round($result,2);
In PHP you can use number_format to control how many decimal places. Assuming you don't want to limit the whole number to just five characters.
echo number_format($numberToFormat, $numberOfDecimalPlaces);
Please better explain your question. Also, what have you tried?
This question already has answers here:
python list in sql query as parameter [duplicate]
(16 answers)
Closed 8 years ago.
I'd like to build a query string with a variable length in where clause.
In PHP I might do this like
<?php
$vars=array('john','mike','matt');
$placeHolders=array_fill(0,sizeof($vars),'%s');
$whereClause=" name in (".join(',',$placeHolders).")";
Is there a concise Python translation of this in python
I think I'd use this to create the variable strings:
', '.join('%s' for _ in vars)
That eliminates the need to substring the result and gets you as many placeholders as you have values.
This question already has answers here:
Add comma to numbers every three digits
(14 answers)
How to format numbers? [duplicate]
(17 answers)
Closed 8 years ago.
Hello there i have problem in jQuery. Suppose i have a value like 20000. And i want the value like 20,000 that after two number a comma will be inserted. I can't do it using jQuery. Please anyone help me out.
Since you said you want the comman inserted "after two numbers", I'm going to assume you have the easy case where you know that your input is a 5 digit number and don't want a more generic number formatting solution. In that case you can do something just like:
var num = 20000;
num = num.toString();
num = num.substr(0,2) + ',' + num.substr(2);
// Now num === "20,000"
Note that 20,000 will only read as twenty-thousand in certain locales. It could be read as just twenty in many locales, but 20000 will be read correctly by anyone who understands Arabic numerals.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to reformat a number with commas
Formatting numbers with thousands separator Smarty PHP
Format a number with grouped thousands
For example the following code:-
{php}echo $youtube->_views;{/php}
returns 2556789 and I want it formatted like this; 2,556,789
This topic says how to do it:
{$myvar|number_format:2:".":","}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Converting a number (1, 2, 3) to a string (one, two, three) in PHP
I have a form in which products will be listed out when,i clich total chk box it will be calculated but,it will be integera only,i want this to print in words.
For eg:217 it should print in words two hundred seven only. Hope,this is clear.
Thnx in advance
PEAR's Numbers Words is old, but competent.
The duplicate question seems to answer this quite well, DAFFODIL, have a look there. You will have to have PEAR installed. Easiest way is to try Numbers_Words::toWords(200) and see if that throws up an error.