I have made a code that takes the first name and last name from an email, the $firstname is uppercase but $lastname is not. Why?
<html>
<body>
<?php
$email = "test.testt#testing.com";
$firstname = ucfirst(strtok(strtok($email, "#"), "."));
$lastname = substr(strtok(strtok($email, "#"), ".") . ' ' . strtok("."), strrpos(strtok(strtok($email, "#"), ".") . ' ' . strtok("."), ' '));
$lastname = ucfirst($lastname);
echo $firstname.$lastname;
?>
</body>
</html>
Output: Test testt
any help would be greatly appreciated
Besides the actual problem being the space, your code does a lot of searching and chopping strings up.
You could simplify it by using explode() with first the # and then a .. Then using ucfirst on each part of the last operation...
$names = explode("#", $email);
// Get first 2 parts of name and split it by the .
[$firstname, $lastname] = explode(".", $names[0], 2);
$firstname = ucfirst($firstname);
$lastname = ucfirst($lastname);
echo $firstname . ' ' . $lastname;
The space was indeed the problem, I fixed it by using $lastname = ucfirst(str_replace(' ', '', $lastname));
Related
Let's say we want to uppercase the first letter in some strings like :
johndev
johnasp
johnphp
johnserver
and we use this for such purpose :
ucfirst(str_replace($name,ucfirst($name),$result['#attributes']['overflows']))
john is our $name variable. It works like this :
Johndev //** these also should be in uppercase, for example : JohnDev
Johnasp
Johnphp
Johnserver
DevJohn
AspJohn
PhpJohn
ServerJohn
How can I fix this?
My solution is:
echo
ucfirst($name)
. ucfirst(substr($result['#attributes']['overflows'], strlen($name)));
Here what you do:
ucfirst $name itself
then ucfirst part of $result['#attributes']['overflows'] which comes after $name
concatenate both parts
For strings like 'PhpJohn' just swap parts:
echo
ucfirst(substr($result['#attributes']['overflows'], strlen($name)))
. ucfirst($name);
Assuming you have $whole and $john, and $name is always the prefix of $whole.
$whole = "johndev";
$name = "john";
$capName = ucfirst($name); // "John"
$tail = substr($whole, strlen($name)); // "dev"
$capTail = ucfirst($tail); // "Dev"
echo $capName . $capTail; // "JohnDev"
If $name could appear anywhere in $whole for any number of times, then you can use:
$whole = "phpjohndevjohnjava";
$name = "john";
$parts = explode($name, $whole); // ["php", "dev", "java"]
$capParts = array_map(ucfirst, $parts); // ["Php", "Dev", "Java"]
$capName = ucfirst($name); // "John"
$answer = implode($capName, $capParts); // "PhpJohnDevJohnJava"
echo $answer;
Finally, I found a way to fix this issue :
$thekey = str_replace($name, '', $result['#attributes']['overflow']);
$ucname = str_replace($name,ucfirst($name),$result['#attributes']['overflow']);
$thename = str_replace($thekey,ucfirst($thekey),$ucname);
echo ucfirst($thename);
I want to grab the last name or second word of this variable
$name = 'Sandra Bullok';
$first_token = strtok($name, ' ');
echo $first_token;
Will output "Sandra"
How can I output "Bullok"
$name = 'Sandra Bullok';
$parts = explode(' ', $name);
echo $parts[0]; // 'Sandra'
echo $parts[1]; // 'Bullok'
http://php.net/manual/en/function.explode.php
If You want to do this with strtok():
$name = 'Sandra Bullok';
$first_token = strtok($name, ' ');
$second_token = strtok(' ');
echo $first_token; // 'Sandra'
echo $second_token; // 'Bullok'
An alternative:
list($first, $last) = explode(' ', $name);
You can do some additional checking to see if how many there are as maybe there is a first, middle and last name.
If you want to use strtok, you should do something like this:
$name = 'Sandra Bullok';
$token = strtok($name, ' ');
while ($token !== FALSE) { // enter all possible values
echo $token . ' ';
$token = strtok(' ');
}
strtok reference
ik have a html form where i can select some options. I want to write those values comma separated to my database. This is the code i have
$genretotal = $_POST['genre'];
$genre0 = $genretotal[0];
$genre1 = $genretotal[1];
$genre2 = $genretotal[2];
$genre3 = $genretotal[3];
$genre4 = $genretotal[4];
$genre5 = $genretotal[5];
$genre6 = $genretotal[6];
$genre7 = $genretotal[7];
$genre = $genre0 . "," . $genre1 . "," . $genre2 . "," . $genre3 . "," . $genre4 . "," . $genre5 . "," . $genre6 . "," . $genre7;
How can i leave out the empty values?
Try with implode and array_filter
implode(',', array_filter($_POST['genre']));
Why so?
$genre = join(',', array_filter($_POST['genre'], function($sItem)
{
//here I assume your 'not empty' matches PHP empty() function
//if not, then add desired conditions
return !empty($sItem);
}));
$genretotal = $_POST['genre'];
if(isset($genretotal) && count($genretotal)>0)
{//This check array is null or not
$gen_arr = implode(",",$genretotal);
}//end if
echo $gen_arr;
//This is the code you avoid empty values
I have 3 arrays $personal1 , $personal2 and $business , each one holds 1 field from each query,
so for example 'Mr' 'John Smith' 'Johnscorp Ltd'.
I am trying to construct the following if query >>
$personal2 = $userinfo->leadname;
$personal1 = $userinfo->salutation;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal1;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
What it does is check to see if the salutation is blank or not, if the field is blank the business name is echo'd instead.
The problem I have is how do I merge the personal and personal2 into one array ?.
I am not sure if I can do this :
$personal2 = $userinfo->leadname;
$personal1 = $userinfo->salutation;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal1 & $personal2;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
or if I can do this ?
$personal = $userinfo->salutation,$userinfo->leadname;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
or if both are incorrect as I dont seem to be getting any results :-S .
I think the first example you have should be working, just replace
$name = $personal1 & $personal2;
with
$name = $personal1 . ' ' . $personal2;
this will join these string with a space between them
PS: Didn't you think strings instead of arrays ?
I'm a bit puzzled because you've stated multiple times that you're using arrays, however in your example, it appears that you're using objects.
Anyways, if all you need is to merge $personal1 and $personal2,
instead of using:
$name = $personal1 & $personal2;
you can just use:
$name = $personal1 + $personal2;
If it's a string you need:
$name = $personal1 . ' ' . $personal2;
array_merge()
or just the + operator
I have names in the form of Lastname, Firstname. In my database I have a different field for both the first and last.
I would like to use PHP to read everything before the comma as the lastname and everything after the comma as the firstname. What is the best way to accomplish this?
list($Lastname,$Firstname) = explode(",",$Name);
<?php
$names = explode( "," , $allNames);
// $names[0] and names[1] are first and last names
?>
with the explode function.
<?php
list($firstname, $lastname) = explode(',','Lastname, Firstname',2);
echo $firstname.' '.$lastname;
?>
If you'll use list();
while( list($fname,$lname) = explode(", ", $db->fetch() ) ) {
echo $lname . " " . $fname . "<br />";
}
Without list() and assining an array;
$name = explode( ", ", $db->fetch()->nameField );
// may be you want to do something with that array
// do something
// echoing
foreach( $name as $fname=>$lname ) {
echo $lname . " " . $fname . "<br />"
}
As nobody has mentioned it yet, to expressly meet the question requirements, you'll need to use the third parameter to explode()
list($lastname, $firstname) = explode(',', $name, 2);