0

My goal and assumption is that a built-in function would generally be faster than a loop that compares two names in a user defined function, as I describe below.

I'm creating an AtoZ set of HTML 'jump-to' buttons using a function that I wrote with server-side PHP code that labels the buttons based on the actual names in a list. To be more helpful, but still keep the list as compact as possible, I show abbreviated partial name ranges like: A-Big, Bil-D, ... where the list starts with a name beginning with A, say Apple, and the first break is between Bigly and Billows. The names in the list are made up of multibyte characters, so any suggested functions need to support multibyte character strings.

The code that I'm using already determines the breaks in the name list based on the number of names on each 'page' of the list, and then loops through the characters in the names before and after each break, comparing each of the characters in the names to find the left-most character position where the difference occurs.

Is there a built-in PHP string compare function that would do this without having to loop through the characters in the two names at each break to determine what to put in the button labels?

If there isn't such a function then, is one to tell me the length of matching characters starting from the beginning of each name?

I looked through the PHP String functions and most of the compare functions only returned less-than, equals, greater-than indicators, not positions/lengths of the difference. The strspn() function comes closer, except that the character order in the two names is important, so using the 2nd name as the mask doesn't seem like it really works.

My looping code works for now, and I'm really looking to make the PHP engine do more of the work for me by 'internalizing' the comparisons, so at this point, I not posting my code.

Thank you.

  • Possible duplicate of [Find first character that is different between two strings](https://stackoverflow.com/questions/7475437/find-first-character-that-is-different-between-two-strings) – LLJ97 Jul 18 '19 at 10:04
  • While it seems to find the first character that is different between two string, it doesn't a/any built-in function(s), which was the point of my question. I'm comparing very long strings, and need to find the first difference as quickly as possible, hence the desire for a likely faster built-in function. Recently. I this need has come up again, but this time I need all of differences and the length of each run of differences because I now need to show the user of a form with a textarea all of the unsupported characters and words entered into the box, and highlight the differences, if any. –  Sep 06 '19 at 01:36

0 Answers0