Get the position of first capital letter in a string using php
php, sorting, string
Solution
You could use `strcspn()`
Find length of initial segment not matching mask
For example:
echo strcspn('aAple', 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');
// prints '1' - the string starts with 1 character not in upper case
Full Code
echo "<pre>" ;
$array = array("aPple", "Orange", "baNana", "mANgo", "Papaya");
foreach($array as $value)
{
echo $value , "= " , strcspn($value, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ') . PHP_EOL;
}
Output
aPple= 1
Orange= 0
baNana= 2
mANgo= 1
Papaya= 0
You use that function to do your (reverse) sort.
Problem
I am having array in which different letter in each word is capital like: aPple, Orange, baNana, mANgo, Papaya I want to get the position of the first letter that is capital. And order them accordingly. That is the word that has first letter capital will be first, and then with second letter capital will be second etc... Like Orange Papaya aAple mAngo baNana If two words come with capital letter in same position for example in above list Orange and Pappaya comes with first letter as capital, then they have to be sorted in alphabetic order. Is this very hard to achieve in php?