3
In PHP I get a phrase from the database, example: Nome do produto teste cadastrado no banco
I need to split her in two like this:
Nome do produto teste
cadastrado no banco
I mean, as much as half as possible, how do you do that? I tried counting the characters and splitting them in half, but you end up cutting a word in half, like this:
$nomeProd = "Nome do produto teste cadastrado no banco";
$nomeCaracter = (strlen($nomeProd))/2;
$nome1 = substr($nomeProd, 0, $nomeCaracter);
$nome2 = substr($nomeProd, $nomeCaracter);
echo $nome1;
echo "<br>";
echo $nome2;
The result was this:
Nome do produto test
e cadastrado no banco
Notice that the word teste
separate, and this could not occur.
a solution would be to count the spaces and divide the sentence in the middle space
– Ademilson Santana da Silva
But you end up cutting a word in half....
– caiocafardo
take a look at the function
substr()
:http://php.net/manual/en/function.substr.php– Thiago Drulla
I explained the difficulty better @Thiagodrulla
– caiocafardo