15
I receive a String through an input and enter it into the database. When I list, I only want the person’s first name, that is, until the first space is found. Is there any function that does this?
15
I receive a String through an input and enter it into the database. When I list, I only want the person’s first name, that is, until the first space is found. Is there any function that does this?
24
You can use the function strstr() because it retouches only the first part of that string. By default the returned value is to the right of the limiter. To change this is to take the value on the left inform true
in the third argument($befero_needle
)
$str = "João da Silva Sauro";
echo strstr($str, ' ', true);
Exit:
João
About the explode() none of the answers commented that from php5.4 it is possible to return the desired index already in the function call.
$str = "João da Silva Sauro";
echo explode(' ', $str)[0];
Very good, rray! Thanks :D
Ingrates, have forgotten the strtok
\0.0/
Can you do that in 5.4? I thought it was 5.5
15
Example:
$nome_completo = "Ciclano Fulano";
$palavras = explode(" ", $nome_completo);
$primeiro_nome = $palavras[0];
print_r($primeiro_nome);
Upshot:
Ciclano
15
Another way:
$str = 'John Doe';
echo substr($str, 0, strpos($str.' ', ' '));
14
It can be as follows:
$string = "Nome Completo";
$string = explode(" ", $string);
echo $string[0];
In this case, only "name"
12
Yeah, just use that example:
// aqui é o campo caminho que vai retornar da consulta.
$caminho ="Faturamento Cupom";
echo "Caminho a ser quebrado<br>".$caminho;
$string = explode(' ', $caminho);
echo "vou imprimir só a primeira parte do caminho ".$string[0];
to print the last part /a/97629/35597
12
One of the many ways is this
$nome = 'João da Silva';
echo preg_replace("#^([^\s]*)\s.*?$#", "$1", $nome); // Exibe João
$nome = 'José';
echo preg_replace("#^([^\s]*)\s.*?$#", "$1", $nome); // Exibe José
5
Then she might get hurt :'(
Forgot to mention the function (not so well known) strtok
.
Check it out:
$nome = 'Wallace de Souza Vizerra';
echo strtok($nome, ' ')
The exit will be:
"Wallace"
Another example is that in versions higher than PHP 5.4, you no longer need to assign a variable to explode
and then print the 0
.
Just do:
$nome = "wallace de souza";
echo explode(' ', $nome)[0]
0
First clear the white spaces on the left.
Then, if it is a user input, it can enter "other characters that are not spaces", such as the invisible character, a ENTER or a TAB. Or it may be that it only type the same first name, so the use of simple PHP functions will fail. Use regular expression. Follow the code:
<?php // Sua string $str = ' Márcio jalber'; // Limpa espaços em branco $strLimpa = ltrim( $str, " \t\n" ); // Limpa a string $strFinal = preg_replace( '/(.*?)[\n\t\s].*/', '\1', $strLimpa );
Regular expression to make something so simple?
Really. After analyzing the question I saw that it is not a user input, but a value that already exists in the bank (and we presume to be treated). Then I agree to replace the last line with the command you mentioned in your reply echo explode(' ', $nome)[0]
Browser other questions tagged php string
You are not signed in. Login or sign up in order to post.
Yes it is possible! D
– rray