2

I'm junior and not at ease with regex, and I'm trying to do a password generator with sentences using regex and preg_split.

All is done except one thing, for example the sentence "I've got 2 cats." should result as "I'vg2c." but the only thing I have is "Ig2c" because I split with white spaces ( preg_split("/[\s]|/", $string, -1, PREG_SPLIT_NO_EMPTY); ) and indeed there isn't any white space between words and special characters.

So is there any ""simple"" option to separate special characters from words and keep it, using regex/preg_split or something else ? :s (Don't know if I'm clear, sorry for my english)

Here is the code :

session_start();


$string = !empty($_POST['sentence']) ? $_POST['sentence'] : NUll;

function initiales($string)
{
  $words = preg_split("/[\s]/", $string, -1, PREG_SPLIT_NO_EMPTY);
  // $words = explode(" ", $string);
   $initiale = '';
   foreach($words as $init){
     $initiale .= $init{0};
   }
  return $initiale;
}
?>



What I want : 

input: initiales("I've got 21 cats and 1 dog!");

expected_output: "I'vg21ca1d!"

unexpected_output: "Ig2ca1d"


  • You have not shared all the code, so it is not quite clear why you get that output. If you need to keep the special chars, use `preg_split("/([[:punct:]])|\s|/", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);` – Wiktor Stribiżew Jan 23 '20 at 09:34
  • Thank you very much. I've edited my post to put the entire code – Marie Selene Bonifacio Jan 23 '20 at 09:45
  • Please reduce the code to showcase the current issue. – Wiktor Stribiżew Jan 23 '20 at 09:57
  • Also add a few more lines: e.g.: `input: initiales("I've got two cats and a dog!");` and on the next line, `expected_output: write_here_what_the_function_should_return`. Add a few examples so we can understand better your issue. – besciualex Jan 23 '20 at 10:06
  • `$words = preg_split("/[\s]/", str_replace("'", " ' ", $string), -1, PREG_SPLIT_NO_EMPTY);` solve de problem? – Benilson Jan 23 '20 at 10:36
  • 2
    Maybe you want `function initiales($string) { return preg_replace('~\b(\w)\w*|\s+~u', '$1', $string); }`? – Wiktor Stribiżew Jan 23 '20 at 10:41
  • @Benilson Yes it works well with the str_replace solution. Many thanks to all, here's the code : `$words = preg_split("/[\s]/", str_replace(array("'", "."), array(" ' ", " . "), $string), -1, PREG_SPLIT_NO_EMPTY);` – Marie Selene Bonifacio Jan 23 '20 at 11:39
  • @WiktorStribiżew I finally tried with your solution in it, and this one works perfectly with both numbers and special characters : `function initiales($string) { return str_replace(' ', '', preg_replace('#(?!\b)([a-zA-Z])(\b)*#', '$2', $string)); }` Thank you ! – Marie Selene Bonifacio Jan 23 '20 at 16:14
  • @MarieSeleneBonifacio Your pattern is very strange, `\b*` makes no sense. What do you mean about numbers? Do you mean you only want to keep the first letter of letter-only words, and you want to keep all numbers? – Wiktor Stribiżew Jan 23 '20 at 17:18
  • @WiktorStribiżew Yes, I wanted to keep the first letters, special characters and all the numbers (I've edited my post with the "expected_output") – Marie Selene Bonifacio Jan 23 '20 at 20:18

1 Answers1

1

You may use

function initiales($string) { 
    return preg_replace('#\B\p{L}\p{M}*+|\s+#u', '', $string); 
}

See the PHP demo

The pattern matches

  • \B\p{L}\p{M}*+ - any letter not at the start of a word + any diacritics after it
  • | - or
  • \s+ - 1 or more whitespaces.

The u modifier is used to match any Unicode whitespace and makes \B Unicode aware.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563