PHP see how many characters are equal - php

I have a string full of values that are separated by a comma. Needless to say this can be done with $strings = explode(',',$fullstring);.
Now, I need to compare a user-submitted-value to this value to see 'how equal' it is. Ideally I'd like to check that maximum one letter/symbol/number/... is not equal. For example, if the hardcoded string is 'table':
'tabl' would be true
'tablz' would be true
'tab' would be false
If you understand what I mean. Of course the strings can also contain spaces, apostrophes, numeric values. I really have no idea how I should get started on this. I've already set up a filter to check whether the value is equal (using if(in_array(strtolower($userValue),array_map('strtolower',$strings)))), but in the else clause I need some method to detect what I just explained.

It sounds like the functionality you're looking for is best served by levenshtein.
Have a look at http://php.net/manual/en/function.levenshtein.php for examples and a description of how to use it.

calculate Levenshtein distance between the two strings: http://php.net/manual/en/function.levenshtein.php and check if the distance is less than some threshold value.

Related

How to display the column value descending when the column having spacial characters in mysql

How to display the column desc order when the column having spacial chars in mysql
I am using the follow query but not display correctly
SELECT quotation_pno FROM crm_quotation order by quotation_pno desc
My output coming like this
quotation_pno
PT/17/999
PT/17/1533
PT/17/1532
PT/16/1531
I want my output like this
quotation_pno
PT/17/1533
PT/17/1532
PT/17/999
PT/16/1531
Please help me
I'd argue, that the output is correct, but your assumptions are not. It looks to me, as if quotation_pno is some kind of textual column, right?
The sorting assumes, that you want to sort text and this works this way:
Set i to 0
Compare the i-th character of two strigns
If they are the same and the end is not reached, increase i by 1 and proceed with step 2
Otherwise order the two strings according to the value at the i-th position
(There are some things elided and the pseudocode is boiled down to the very basic, needed to understand the principle).
Applied to your example this means, when the comparison compares PT/17/999 and PT/17/1533 it looks at the characters 0 to 5 and "sees" that they are equal. When it compares the characters at position 6, they are '9' and '1'. Since the character '9' is considered to be greater than '1', PT/17/999 is placed before PT/17/1533.
How to solve the issue?
There are some ways coming into my mind, that will allow you to achieve the desired sort order.
First, you could prepend the numbers with zeros. This will allow you to re-use most of your existing structure, but will result either in very many zeros, or a system that is somehow limited, since you will be restricted to the number of digits you decided to use (or the sort will fail again).
The second possibility is, to store the parts in (additional) numerical columns in the table, e.g. one for year and one for the order number in this year. This is the more flexible approach, but involves more changes.

MySql Php Find Similar Values

I run a website where users have a username. They can change their usernames whenever they want. When them change their name, we check that that name isn't currently being used and then allow or not allow the change. On our site people often like to change their username to copy other peoples (make their name very similar to confuse other people of their identity). This isn't uncommon for the type of site we run.
Is way to easily check for usernames that are somewhat similar using a simple query?
Here are some examples of usernames that we would like to have a query match up.
testingman1 = testingman11
lionhead = Iionhead (one has an l and the other has a capital i)
sleepybears = sleeepybears
Any way to do a character by character count of the same letters in the same position and then determine based on the percentage if it is a copy of another user?
I know I'll most likely have to write a custom function, but just looking for some advice on how to make it as painless and not very system taxing process.
You can use
levenshtein(str1, str2) that will return an integer witch is the distance between the two strings.
In PHP if one string is longer that 255 characters the function will return -1.
More info: http://php.net/manual/en/function.levenshtein.php
or if you want in percent you can use similar_text ( string $first , string $second, [, float &$percent ] )
witch pass in the 3rd parameter the percent of similarity
More info: http://www.php.net/manual/en/function.similar-text.php

Regex to search single field for less than values and string

This is probably a lot less complicated than it seems right now, but looking for help. I am trying to write a regex to search a single field in a MySQL DB.
Basically: It should work like this - if you search for:
*Term = Result*
250 Cattle = anything less than or equal to 250 "Cattle"
235 Sheep = anything less than or equal to 235 "Sheep"
The values in the DB look exactly like the term being searched (i.e. "250 Cattle, 300 Horses" would be considered 1 entry)
So it needs to do an operation on a # (i.e <= 250) and be able to specify the category. "Sheep", "Cattle", etc all in the same regex or two conditional regex's. But I haven't figured it out yet. Any help would be amazing.
Why do you need to use regex for this ??
Could'nt you just put every thing in an array (depending on your programming language) or list like so:
Key:250 value:Cattle
Key:300 value:Horse
and then use the reference on the key to compare each other with a loop trought the array/list?
Comparing numbers is way more easy with if then with Regex.

Select alphabetically "nearest" option from a dropdown

I have a list of words in a dropdown and I have a single word that is looking for a suiting partner(user is choosing it)
To make this easier for the user(because the list can be very long and the porcess has to be fast) I want to give a possible option.
I already had a look how i van change the selected word.
I want to find the alphabetically "nearest" option but i have no idear how i could find out which word is the nearest neigbore....
I already googled with all words I could think of to get a solution but I couldnĀ“t find something.
Does someone have an idear how i can do it?
The levenshtein function will compute the 'closeness' of 2 string. You could rank the words you have relative to user's string and return the string with the lowest value.
have a look at this library, it contains Fuzzy string matching functions for javascript, including stemming, lehvenstein distance and metaphones: http://code.google.com/p/yeti-witch/
If by alphabetically you mean matching letters read from the left, the answer is easy. Simply go through every letter of the word and compare it with the ones in the select drop down. The word that shares the longest starting substring is your "nearest".
The simplest (and probably fastest) thing in javascript is finding (by binary search) where to put the word in sorted array of your option words using < and > string operators.
For more advanced and precise results, use Levenshtein distance

PHP, find exact value from mysql arranged data

I have a data in My-Sql column like this
T_interest
1,14,49,145,203,302
It represents each value for personal interest keywords.
I tried to extract the value and distinguish whether it has the value or not for the checkbox.
if(strstr($u_interest['u_interest'], ','.$row['i_idx'])):
$selected = 'checked';
here is the php command that I use right now.
but it doesn't extract exact value from the database.
Let's say I want to check if the data has 14 number or from this user table
T_interest
1,14,49,145,203,302
and if I use above command it tells me that a user has two values.
14,145
It looks like PHP strstr command tells me two values because these two have 14 number.
So, can you help me why this is happening?
If you want more php lines I can post them.
Explode it into an array, this way you get the individual values.
explode(",",$t_interest["u_interest"]);
Then you can test for equality much easier.
You really need to read up on database normalization. A properly normalized design would make this problem moot.
As for your question, since you're forced to use string operations, you'll have to check for multiple different cases:
1) the number you want is at the START of the string
2) the number you want is at the END of the string
3) the number you want is the ONLY number in the string
4) the number you want is in the MIDDLE of thee string:
SELECT ...
WHERE
(T_Interest = 14) OR // only number
(T_Interest LIKE '14,%') OR /// at the begnning
(T_Interest LIKE '%,14') OR // at the end
(T_Insertest LIKE '%,14,%') // in the middle

Categories