-2

How can I split this string:

#now my time #Cairo travel #here

to become an array like this:

Array (
    [0] => #now
    [1] => my time
    [2] => #Cairo
    [3] => travel
    [4] => #here
)

All words starting with a hash symbol can only be one word long.
Words that do not start with a hash symbol should be kept together as a phrase.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
M.Ahmed
  • 67
  • 1
  • 9
  • Is this string a variable? Or is it always the same? – GrumpyCrouton Oct 31 '17 at 13:43
  • What code did you try to use to split the string? – VTodorov Oct 31 '17 at 13:44
  • 1
    The answer i provided was exactly the one you wanted for your example. Obviously you have a mistake in what you ask because you accepted the wrong answer based on what you ask. Please next time be more careful. – pr1nc3 Oct 31 '17 at 14:04

3 Answers3

0
<?php

$string='#now my time #Cairo travel #here';
$string=explode(' ',$string);

$result=array($string[0],$string[1].' '.$string[2],$string[3],$string[4],$string[5]);
print_r($result);


Array ( [0] => #now [1] => my time [2] => #Cairo [3] => travel [4] => #here )
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0
function extractHashes($string) {
  $return = [];
  $r = 0;
  $parts = explode(' ', $string);

  // Helper function return bool if string starts with hash.
  $swh = function ($s) {
    return (strpos($s, '#') === 0);
  };

  for ($p = 0; $p < count($parts); $p++) {
    // Add word or append word to return array.
    if (!isset($return[$r])) {
      $return[$r] = $parts[$p];
    } else {
      $return[$r] .= ' ' . $parts[$p];
    }

    // Increment index if current word or next word starts with hash.
    if ($swh($parts[$p]) || ($p + 1 < count($parts) && $swh($parts[$p + 1]))) {
      $r += 1;
    }
  }

  return $return;
}

Your example:

$string1 = "#now my time #Cairo travel #here";
print_r( extractHashes($string1) );

Outputs:

Array
(
    [0] => #now
    [1] => my time
    [2] => #Cairo
    [3] => travel
    [4] => #here
)

Another example:

$string2 = "here is #another #string for testing #function works";
print_r( extractHashes($string2) );

Outputs:

Array
(
    [0] => here is
    [1] => #another
    [2] => #string
    [3] => for testing
    [4] => #function
    [5] => works
)
labue
  • 2,605
  • 3
  • 26
  • 35
0

When dealing with variable delimiters, regex is a sensible choice.

I recommend preg_split() to instantly create a flat array of values. The pattern maintains that words starting with # may not contain a space and if a word does not start with #, then it should contain all subsequent words that do not start with #.

Code: (Demo)

var_export(
    preg_split(
        '/(?:#\S+|[^# ]+(?: [^# ]+)*)\K /',
        '#now my time #Cairo travel #here'
    )
);

Output:

array (
  0 => '#now',
  1 => 'my time',
  2 => '#Cairo',
  3 => 'travel',
  4 => '#here',
)

Breakdown:

#/(?:#\S+|[^# ]+(?: [^# ]+)*)\K /
#    ↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↳match literal space
#    ↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↳↳forget any previously matched characters
#    ↓↓↓↓ ↳↳↳↳↳↳↳↳↳↳↳↳↳↳↳↳↳↳↳match non-hashed words separated by space
#    ↳↳↳↳match hashed word

For a more in-depth break down of the pattern syntax, try it at regex101.com

mickmackusa
  • 43,625
  • 12
  • 83
  • 136