Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the position of first capital letter in a string using php

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?

like image 918
esafwan Avatar asked Apr 20 '12 07:04

esafwan


People also ask

How can I get first letter capital 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.

How do you get a capital letter from a string?

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.

What is Strtoupper?

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.

How do you capitalize words in PHP?

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.


4 Answers

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.

like image 159
Ja͢ck Avatar answered Nov 13 '22 12:11

Ja͢ck


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.

like image 36
Robin Castlin Avatar answered Nov 13 '22 14:11

Robin Castlin


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

like image 30
Matthias Tylkowski Avatar answered Nov 13 '22 12:11

Matthias Tylkowski


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

like image 29
CyprUS Avatar answered Nov 13 '22 12:11

CyprUS