diank
diank

Reputation: 638

PHP Crushing/Explode text

I have a one input that is used for a "Name, Surname and Family name". But I want if user enter more than 3 names, all after 3(4,5,6...) to be like family. But if you submit 2 names the Family name must be "0".

Here is my code:

<form method="POST" action="getNames.php">
    Names: <input type="text" name="names" />
    <input type="submit" name="submit" />
</form>

<?php

$names = $_POST['names'];

// some action here to get names

$firstName = ""; // Get first name
$surName = ""; // Get Sur name
$familyName = ""; // Get family name

?>

I will be very glad if someone help me. Thanks!

Upvotes: 0

Views: 54

Answers (3)

TarranJones
TarranJones

Reputation: 4242

This what your looking for?

$_POST['names'] = '   Arthur     Ignatius       Conan  Doyle   ';

$names = trim(preg_replace('/\s+/', ' ', $_POST['names']));

$parts = explode(' ',$names , 3);

list($firstNamm, $surName, $familyName) =  array_replace(array_fill(0,3,''), $parts  );

Explanation,

preg_replace('/\s+/', ' ', $_POST['names']) is used to reduce multiple spaces down to a single whitespace.

from

'   Arthur     Ignatius       Conan  Doyle   '

to

' Arthur Ignatius Conan Doyle '

trim() is used to remove whitespace from the beginning and end of a string, you can provide a second argument if you wish to remove other characters.

which now gives us

'Arthur Ignatius Conan Doyle'

explode() splits a string into an array when given a delimiter (" "), the 3rd arguments let you set the maximum number of parts to split the string into.

as the string may not have 3 parts its possible that an empty array is returned. array_fill(0,3,'') creates an array with three elements all set to an empty string. array_replace(array_fill(0,3,''), $parts ); returns the array but with values replaced with any values provided by the $parts array.

This array is passed to the list construct which populates a list of variables with the values of each array element.

Upvotes: 1

kamal pal
kamal pal

Reputation: 4207

You can use explode with a space, and use array_filter on that to remove blanks, as user may input two spaces.

There after you can use array_shift which return you a value from top of an array.

and later check if array isn't empty, then use implode with spaces otherwise set $familyName to 0

<?php
$names = 'John Doe';
$arrNames = array_filter(explode(' ', $names));

echo $firstName = array_shift($arrNames) . PHP_EOL;
echo $surName = array_shift($arrNames) . PHP_EOL;
echo $familyName = empty($arrNames) ? '0' : implode(' ', $arrNames);

Upvotes: 1

Sofiene Djebali
Sofiene Djebali

Reputation: 4508

This code will output an array of familly, if an input is missing, it will be replaced with a 0.

PHP

<?php
$_POST['names'] = 'sofiene, totose, tata, titi';
$names = explode(',', $_POST['names']);
$familly = [];

for ($i = 0; $i < count($names); $i+=3) {
    $familly[$i] = [
        'firstName' => (isset($names[$i])) ? $names[$i] : 0,
        'surName' => (isset($names[$i + 1])) ? $names[$i + 1] : 0,
        'familyName' => (isset($names[$i + 2])) ? $names[$i + 2] : 0
    ];
}
var_dump($familly);
?>

EvalIn

Upvotes: 1

Related Questions