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?
The ucfirst() function converts the first character of a string to uppercase. Related functions: lcfirst() - converts the first character of a string to lowercase. ucwords() - converts the first character of each word in a string to uppercase.
To find all the uppercase characters in a string, call the replace() method on the string passing it a regular expression, e.g. str. replace(/[^A-Z]/g, '') . The replace method will return a new string containing only the uppercase characters of the original string.
The strtoupper() function is used to convert a string into uppercase. This function takes a string as parameter and converts all the lowercase english alphabets present in the string to uppercase. All other numeric characters or special characters in the string remains unchanged.
The ucwords() is an in-built function of PHP, which is used to convert the first character of each word to uppercase in a string. The ucwords() is supported by the PHP 4 and above versions. It takes a string as an input and converts the first character of each word of the string to uppercase.
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.
preg_match("/^.*?[A-Z]/", 'aAple', $arr);
$str_position = (strlen($arr[0]) - 1);
There are so many ways you can do this on. Use the one you find most logical. :)
This one simply returns the first part of the string to the first Capital letter. Then I calculate string length and subtract by 1.
If you use PHP > 5.3 you can use this:
$test = array("aPple", "Orange", "baNana", "mANgo", "Papaya");
echo implode(', ', array_map(function($value){
$matches = array();
preg_match('/[A-Z]/', $value, $matches, PREG_OFFSET_CAPTURE);
return $matches[0][1];
}, $test));
Otherwise you have to create another function as replacement for the lambda function.
Output: 1, 0, 2, 1, 0
If you want a non-fancy way to do it , here is my suggestion :
** for gettings caps position *
function getCapsPosn($str)
{
$i = 0;
$CapsPosn = -1;
for ($i =0 ; $i < strlen($str) ; $i++)
{
if (( ord($str[$i]) >= 65 ) && ( ord($str[$i]) <= 90))
{
//echo ord($str[$i]);
$CapsPosn = $i;
return $CapsPosn;
}
}
}
Use an associative array to store the Caps posn in the array .e.g. array['aPple'] = '1' and so on. then use php sort functions to sort it. PHP sorting functions can be found at : http://php.net/manual/en/array.sorting.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With