I have code where I am extracting a name from a database, trying to reorder the word, and then changing it from all uppercase to word case. Everything I find online suggests my code should work, but it does not... Here is my code and the output:
$subjectnameraw = "SMITH, JOHN LEE";
$subjectlname = substr($subjectnameraw, 0, strpos($subjectnameraw, ",")); // Get the last name
$subjectfname = substr($subjectnameraw, strpos($subjectnameraw, ",") + 1) . " "; // Get first name and middle name
$subjectname = ucwords(strtolower($subjectfname . $subjectlname)); // Reorder the name and make it lower case / upper word case
However, the output looks like this:
John Lee smith
The last name is ALWAYS lowercase no matter what I do. How can I get the last name to be uppercase as well?
The above code gives wrong results when there are multibyte characters in the names like RENÉ. The following solution uses the multibyte function mb_convert_case.
$subjectnameraw = "SMITH, JOHN LEE RENÉ";
list($lastName,$firstnName) = explode(', ',mb_convert_case($subjectnameraw,MB_CASE_TITLE,'UTF-8'));
echo $firstnName." ".$lastName;
Demo : https://3v4l.org/ekTQA
Related
The scenario is there is a given sentence. The user have to type in the sentence in some input field and that will be saved to database.
Now, I want to compare both the sentences and find out, how many words the user has typed in correctly and incorrectly (missed any word, or added some new words by typing mistake).
Now I have done something like this-
$Str1 = "i **am** suraj roy i **am** having my lunch";
$Str2 = "i suraj roy i **am** having my dinner";
$st1 = (explode(" ", $Str1));
$st2 = (explode(" ", $Str2));
$result1 = array_diff($st1, $st2);
$result2 = array_diff($st2, $st1);
$word_count1 = count($result1); // words not present in typed sentence from original sentence - lunch
$word_count2 = count($result2); // newly added words in typed sentence - dinner
$total_error_count = $word_count1 + $word_count2;
echo 'Total words not matching between two sentences - '.$total_error_count;
Now this is working as i expected. Good.
But the problem is, have a look at the word am in both sentences, as the comparison is being done between array its taking single occurrence of am in typed sentence for all the occurrence of am in original sentence. So, there is no missing word count for am in typed sentence.
So, any help would be appreciated. Thanks.
I am trying to find the last name from full names with trailing credentials. Some names have middle names or initials, hyphenated names, multiple credentials, and some have no trailing credentials:
John Doe, MD
Jane X. Doe, FNP-C
Joe Cool, MD, PhD
Billie-Jo Last
I've started with
/\w+(?=[\s,]+\S*$)/gmi
which gets the last names of the first 2 formattings, but picks up the 1st credential and first name of the second 2 formattings, respectively.
Thank you for all your help.
If you are 100% confident that they all follow the format you posted, you can do this per row (per person):
// Get the names part of the string
$parts = exlode(',', $nameString);
// The first element is the names.
// Now, split the name string to get the names
$names = explode(' ', $parts[0]);
$first = $names[0];
$middle = null;
$last = null;
if (count($names) == 2) {
// We only have two names, which probably means that
// the second is the last name
$last = $names[1];
}
if (count($names) == 3) {
// We got three names, let's assume that the second is the middle name
$middle = $names[1];
// And the third is the last name
$last = $names[2];
}
The above code can be optimized, but I wanted to make it as self explanatory as possible.
Note: This only works for the names that follows the format you mentioned. If you were to get more than three names, you have a problem since you won't be able to determine if it is a double first, middle or last name.
If all your input is well structured as you described, then the best advice is to do what #Magnus Eriksson stated in his commend. The code is extremely simple:
$parts = explode(',', $inputString);
$names = explode(' ', trim($parts[0]));
$lastName = $names[count($names) - 1];
i want to get a word until a spesicif character, example space. i trying to use substr but i cant.
Assumming i have column data :
1. 3/14/2016 13:46
i would like to get 3/14/2016 and showing it. is that possible?
2. 3/14/2016 13:46
i would like to get 13.46 without date. is that possible?
can someone tell me how to get string from 2 case above.
thanks a lot
If the columns are space delimited then you can use an explode and pull the index of the column of which you want.
$rowData = "1. 3/14/2016 13:46";
$rowCols = explode(" ",$rowData);
echo $rowCols[1]; // returns 3/14/2016
echo $rowCols[2]; // returns 13:46
$specifidCharacter = " " //Space for example
$string = "1. 3/14/2016 13:46";
$tmp = explode($specificCharacter, $string);
echo $tmp[1] //Case 1
echo $tmp[2] //Case 2
I'd like to know How to Search for Space in Php
For example , I've got $Studentname and it's value is John Green . I want to have $Name (with value John) And $Surname(With value Green) . But i don't know how to search space in string .
Thanks in advance
Using explode() without a limit will result in incorrect results. Some first and last names have spaces. For example:
$name = explode(" ","Jan Van Riebeck");
echo $name[0]." ".$name[1]; // Jan Van (incorrect)
$name = explode(" ","St. John Westcox");
echo $name[0]." ".$name[1]; // St. John (incorrect)
Use explode() with a limit (so it only returns 2 items), like so:
$name = explode(" ","Jan Van Riebeck",2);
echo $name[0]." ".$name[1]; // Jan Van Riebeck (correct)
This will still be incorrect some time, but more last names have spaces than first names. Ideally, if you're capturing the form data, use two different fields for first and last name, but even this isn't always ideal, some cultures have different ways that names work that aren't as simple as first name last name.
Here is a list of common pitfalls when it comes to working with names
First suggestion:-
Try to take first name and last name in two different fields when you are using html form.
Use below code if the above not possible:-
<?php
$Studentname = 'John Green';
$student_full_name = explode(' ',trim($Studentname));//trim to remove space from starting and ending and explode to break string when space found
$first_name = $student_full_name[0];//output John
$sur_name = $student_full_name[1];//output Green
?>
Note:- What happen if a user put name like "John Green smith" Or "John(more than one space)Green". That's why do first point.
You can use the explode() function to split a string into an array by using the space as a delimiter:
$names = explode(" ", "John Smith");
However, you might want to take account of people doing silly things like putting double spaces between the first name and last name, or having preceding or trailing spaces.
You can use trim() for getting rid of trailing and preceding spaces:
$fullName = " John Smith ";
$trimmedName = trim($fullName); // Assigns "John Smith"
You can also use preg_replace() to get rid of multiple spaces between the first and last name:
$fullName = "John Smith";
$cleanedName = preg_replace("/\s+/", " ", $fullName); // Assigns "John Smith"
The above code will replace one or more (+) consecutive spaces (\s) with a single space.
to get name and username from student name follow this
$Studentname = 'John Green';
$name = explode(' ',$Studentname);
$firstname = $name[0]; //output john
$lastname = $name[1]; //output green
I am having trouble to separate the text.
This is the scenario:
$check = "Apple|Orange|Animal|Dog|Grape";
Suppose by using explode i could separate the word with "|", but because the value i retrieved "Animal|Dog" should be a word so in this case, what would be the solution?? I could not use
limit as well because the position or number of text could be different.
The only way to distinctly separate the text is the Animal keyword. Is there any function in php that similar to mysql LIKE syntax?
If Case 2
$check = "Apple|Orange|Animal:Dog|Cat|Grape";
OR
$check = "Apple|Orange|Animal:Fish|Bird|Grape";
where the name of animal could be vary.
Output
"Apple|Orange|Animal:Dog,Cat|Grape" or "Apple|Orange|Animal:Fish,Bird|Grape"
Thanks.
If all you want to do is replace "Animal|" with "Animal:" then you can do a simple str_replace:
$check = "Apple|Orange|Animal|Dog|Grape";
$newCheck = str_replace("Animal|","Animal:"); // will be set to 'Apple|Orange|Animal:Dog|Grape'
Is that what you meant?
EDIT, FOR CASE 2:
I assume you have a string like "Apple|Orange|Animal:Dog|Cat|Grape", which has the category followed by 2 members of the category. From what you've said, you want to transform this string into "Apple|Orange|Animal:Dog,Cat|Grape" with a comma separating the two group members instead of a pipe. This is more complicated than the first case - the category name could vary, and you can't do a simple str_replace starting with the colon because the first member of the group could vary as well. For this case, you'll need to use a regular expression to match and replace the pattern of the string. Here's the code:
$check = "Apple|Orange|Animal:Dog|Cat|Grape";
$newCheck = preg_replace("#(Animal:\w+)\|#", "$1,", $check); // will be set to "Apple|Orange|Animal:Dog,Cat|Grape"
DEMO
Let me explain what this does, in case you're not familiar with regular expressions. The first argument of the preg_replace function, "#(Animal:\w+)\|#", tells PHP to look for all substrings of $check that begin with the text "Animal" followed by a colon, then a string of words with one or more character, and end with a pipe. This will look for the category name as well as the first member of that category in your string. The second argument, ":$1,", tells PHP to change the first pipe after this pattern into a comma. If you have a different category name, simply change the pattern you pass as the first argument to the preg_replace function:
$check = "Apple|Orange|Animal1:Fish|Bird|Grape";
$newCheck = preg_replace("#(Animal1:\w+)\|#", "$1,", $check); // will be set to "Apple|Orange|Animal1:Fish,Bird|Grape"
Let me know if this is hard to follow!
The way I would handle this would be to use a / instead of a | between categories and items. Or use a different delimiter if you really want to keep the | in between categories & items.
$check = "Apple|Orange|Animal/Dog|Grape";
$ex=explode("|", $check);
but if you don't want to do that... then if you know what your category names are like "animal", you could explode the array on | and assume that if your current value is "animal", then the next element in the array is going to be "dog", "cat", whatever. This is not a good solution though, and would not work for multiple category levels.
You have spaces between Apple and orange, like this Apple | Orange. But there is no space between Animal and Dog Like this Animal|Dog. If this is the situation, you can explode it like this
$check = "Apple | Orange | Animal|Dog | Grape";
$ex=explode(" | ", $check);
Which will return array in format
array("Apple","Orange","Animal|Dog","Grape");
You can manipulate above array again to get Animal
I hope this is what you meant
Edit : A rough solution could be :
<?
$check = "Apple|Orange|Animal|Dog|Grape";
$ex=explode("|",$check);
if(in_array("Animal",$ex))
{
echo "Animal:";
}
if(in_array("Dog",$ex)){
echo "Dog";
}
?>
So in this case the position of Animal and Dog doesnot matter
str_replace("Animal|","Animal:",$check);
then do explode