Detecting an uppercase letter in a word - PHP
Solved
Anonymous user
-
Anonymous user -
Anonymous user -
Bonjour,
I am currently looking for a "one-size-fits-all" script. What I mean is that once set up, I would just need to follow a naming convention for the script to work all the time.
The case is as follows: I have a table named "person".
It can contain X fields.
I have a page that displays the current information and at the same time a form to modify them.
My naming convention is as follows:
[information]_[table name]
example: firstName_person,
postalCode_person
etc..
Current code:
data is an array containing the information of a person from the table.
The problem is that visually the form is not very explicit, as it shows:
postalCode 74000 [empty text input]
I would like instead of postalCode to display: Postal Code
My first idea was to name my field like this:
postalCode_person. But I can't find a PHP function to detect the uppercase.
We could then put an array containing the 24 letters of the alphabet in uppercase and use the explode function. But wouldn’t that be heavy?
Otherwise, to write "code-postal_person" and again use explode.
But isn’t it discouraged to name a field this way in the table?
2)
As for the firstName field: I would like to put the accent to get: First Name.
Can I put it directly in the database? Otherwise, it seems impossible to add this accent unless specified in the script.
And in the end, if there are 10 fields with accents I would have to do it for all 10 fields which is not great!
Thank you to those who read this post :).
I am currently looking for a "one-size-fits-all" script. What I mean is that once set up, I would just need to follow a naming convention for the script to work all the time.
The case is as follows: I have a table named "person".
It can contain X fields.
I have a page that displays the current information and at the same time a form to modify them.
My naming convention is as follows:
[information]_[table name]
example: firstName_person,
postalCode_person
etc..
Current code:
data is an array containing the information of a person from the table.
foreach($data as $key => $element) { $words = explode("_", $key); //separating words at each "_" /* Here we get: word[0] = postalCode word[1] = person */ //I display my label for each field <label for=" ' .$key. ' "> ' .$words[0]. ' </label> } The problem is that visually the form is not very explicit, as it shows:
postalCode 74000 [empty text input]
I would like instead of postalCode to display: Postal Code
My first idea was to name my field like this:
postalCode_person. But I can't find a PHP function to detect the uppercase.
We could then put an array containing the 24 letters of the alphabet in uppercase and use the explode function. But wouldn’t that be heavy?
Otherwise, to write "code-postal_person" and again use explode.
But isn’t it discouraged to name a field this way in the table?
2)
As for the firstName field: I would like to put the accent to get: First Name.
Can I put it directly in the database? Otherwise, it seems impossible to add this accent unless specified in the script.
if(firstName_person == $key){ words[0] = "first name"; } And in the end, if there are 10 fields with accents I would have to do it for all 10 fields which is not great!
Thank you to those who read this post :).
4 answers
-
With this, you find in $result the first uppercase letter of $data...
if (preg_match('#^[^A-Z]*([A-Z])#',$data,$result)) $result=$result[1]; else unset($result);
With this, you find in $result the first uppercase letter as well as the entire sequence of characters of $data...
if (preg_match('#^[^A-Z]*([A-Z].*)#',$data,$result)) $result=$result[1]; else unset($result);
Abandon what troubles you in favor of what soothes your heart.-
-
function explode_maj($t=false){ if ($t) return explode(' ',trim(preg_replace('#([A-Z])#',' $1',$t)));} $k=explode_maj('HelloThisIsATestToSeeIfTheFunctionDividesWellALongTextLol'); print_r($k); $k=explode_maj('ApparentlyItWorksEVENWithASingleCharacterOrNumbers57320ForExample'); print_r($k); $k=explode_maj('Lol'); print_r($k);
returnsArray ( [0] => Hello [1] => This [2] => Is [3] => A [4] => Test [5] => To [6] => See [7] => If [8] => The [9] => Function [10] => Divides [11] => Well [12] => A [13] => Long [14] => Text [15] => Lol ) Array ( [0] => Apparently [1] => It [2] => Works [3] => E [4] => V [5] => E [6] => N [7] => With [8] => A [9] => Single [10] => Character [11] => Or [12] => Numbers57320 [13] => For [14] => Example ) Array ( [0] => Lol )
-
-
To give up what troubles you in favor of what calms your heart.
-
I keep re-reading the documentation, but I don't understand what the $1 in preg_replace is for.
Are you replacing all capital letters with ' $1' (a space and variable 1)?
And then with explode, do you replace ' $1' with a space?
And finally, do you replace all spaces with ',' to put them in the array called $k? -
-
-
You can create a function that uses a regex to match all pairs `[information]_[table name]`. The function rewrites the field `[information]` and returns the result...
--
Give up what troubles you in favor of what soothes your heart. -
@ktm
Thank you very much, I was able to use your function. It perfectly meets the expectations.
if (preg_match('#^[^A-Z]*([A-Z].*)#',"codePostal",$result)){ $result = $result[1]; echo $result; } // If others read this post, to display the array: echo '<pre>'; print_r($result) ; echo '</pre>';
However, I tried with another word. Let's say: motDePasse.
To separate "de" and "passe": do we relaunch the function?
I thought preg_match_all would do the trick. But it doesn't seem to work, unless I used it incorrectly?-
With this you find in the array $result the first uppercase letter of the first word and the first uppercase letter of the second word from $data...
if (preg_match('#^[^A-Z]*([A-Z])[a-z]*?([A-Z])#',$data,$result)) $result=array($result[1],$result[2]); else unset($result);
With this you find in the array $result the first uppercase letter and all the lowercase letters of the first word and the first uppercase letter and all the lowercase letters of the second word from $data...
if (preg_match('#^[^A-Z]*([A-Z][a-z]*?)([A-Z][a-z]*?)#',$data,$result)) $result=array($result[1],$result[2]); else unset($result);
With this you find in the array $result the first uppercase letter and all the lowercase letters and numbers of the first word and the first uppercase letter and all the lowercase letters and numbers of the second word from $data...
if (preg_match('#^[^A-Z]*([A-Z][a-z0-9]*?)([A-Z][a-z0-9]*?)#',$data,$result)) $result=array($result[1],$result[2]); else unset($result);
With this you find in the array $result the first uppercase letter and the rest of the first word and the first uppercase letter and the rest of the expression $data...
if (preg_match('#^[^A-Z]*([A-Z][^A-Z]*?)([A-Z].*)#',$data,$result)) $result=array($result[1],$result[2]); else unset($result); -
Indeed, the function plays its role. I finally understood how regex works globally.
But I have some unclear areas in what you provided.
^[^A-Z]* --> at the beginning of the string, there should be no uppercase letters.
([A-Z][^A-Z]*?) --> in variable number 2: we should find an uppercase letter followed by lowercase letters (0, 1, or several times = *), but what is the purpose of the ? (since it also says that it's optional?)
Finally, my final script:<?php $text = "UnJourMonAncetreGurdil"; //We remove all lowercase letters $majuscule = preg_replace('#[a-z]#','', $text); //We count the number of uppercase letters $nbrMajuscule = strlen($majuscule); /* Regex that allows retrieving an uppercase letter and lowercase letters, do not forget the # see Site du zero Memento des expressions régulières */ $regexInitiale='([A-Z][^A-Z]*)'; $regexFinale = ' '; //We create the regex by adding the initial letter for each word FOR($iBcl=0;$iBcl<$nbrMajuscule;$iBcl++){ $regexFinale .= $regexInitiale; } //We add the # on each side $regexFinale = "#" . $regexFinale . "#"; //We perform the query if (preg_match($regexFinale,$text,$result)){ //The first line is deleted as it contains the entire variable $text unset($result[0]); /* Display the result array echo '<pre>'; print_r($result) ; echo '</pre>'; */ }else{ unset($result); } ?>
A big thank you for your help :). -
All your script =
$text = 'OneDayMyAncestorGurdil'; $result = explode(' ',trim(preg_replace('#([A-Z])#',' $1',$text)));
exampleHelloThisIsATestToSeeIfTheFunctionProperlySplitsAVeryLongTextLol
returnsArray ( [0] => Hello [1] => This [2] => Is [3] => A [4] => Test [5] => To [6] => See [7] => If [8] => The [9] => Function [10] => Properly [11] => Splits [12] => A [13] => Very [14] => Long [15] => Text [16] => Lol )
-
-