regex - PHP - Extract characters before and after first space

644

Can't seem to get this one. I would like to get the first 2 characters from the word before the first space and first 3 characters after the first space.

Not sure if break up the word in [0], [1] would work, but regex might work.

$string = "Bobby Ryan";
$output = 'BoRya';

Thanks

103

Answer

Solution:

What have you tried?

suggestion: explode() your string on the space character, get the first and last values in the array by index, then use substr() to fetch the characters you require.

277

Answer

Solution:

This should work correctly.

   $string = 'Bobby Ryan';

   $words = explode(" ", $string);
   $output = substr($words[0], 0, 2) . substr($words[1], 0, 3);

   echo $output;
579

Answer

Solution:

Fromexplode()'s manual entry: (http://www.php.net/explode) -array explode ( string $delimiter , string $string [, int $limit ] ).

the optional$limit parameter indicates into how many pieces the string should be exploded to, and considering what you want to achieve, along withsubstr()(http://www.php.net/substr):

list($beforeSpace, $afterSpace) = explode(" ", $string, 2);
$output = substr($beforeSpace, 0, 2) . substr($afterSpace, 0, 3);

will break the string into two strings right at the first space character, and then form the output from the first two characters of whatever was before the space, and the first three characters of whatever was after the space.

760

Answer

Solution:

$string = "Bobby Ryan";
$var=explode(" ",$string);
// Totally didnt debug this part, may have to change 2 and 3 to -1 to 1 and 2
$output = substr($var[0],0,2) +  substr($var[1],0,3);
181

Answer

Solution:

If you want to use regex only:

$string = "Bobby Ryan";
$output = preg_replace("/(\w{2})\w* (\w{3})\w*/", "$1$2", $string);
663

Answer

Solution:

You must first split the string into parts. Look at explode or some kind of String tokinizer. You must then use a substring function to get parts of the strings.

$string = "Bobby Ryan";
$output = getPartsOfString($string, 2, 3);
echo $output;

function getPartsOfString($string, $firstNumOfChars, $secondNumOfChars) {
{
    $part = explode(" ", $string);

    // Get substrings from parts
    return substr($part[0], 0, $firstNumOfChars).substr($part[1], 0, $secondNumOfChars);
}

The function could have several more parameter. The start of first and second string. (The 0s in substring function) and a spesified delimiter. (Change to explode($delimiter, $string);

Hope this helps.

People are also looking for solutions to the problem: php - allow datepicker to pick values only upto two weeks from the start date

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.