4

I have some strings like this

", One "
", One , Two"
"One, Two "
" One,Two, "
" ,Two ,Three "

EDIT 2: Some strings have two words between coma like ", Two ,Three, Twenty Five, Six".

and need to remove space and or comma at the beginning and end of the string only tried few regex with preg_replace(), but they replace all occurrences.

EDIT: Actually would be great to remove all clutter like !@#$%^&*( etc whatever is at the end and beginning of string, but not in between.




Optionally need to make strings look proper by placing word then comma then space then another word (if there's comma one in between words).

Example "One,Two ,Three , Four" into "One, Two, Three, Four".

P.S. Please provide answer as two separate regex as its easier to understand.

Alex
  • 163
  • 7
Roman Toasov
  • 747
  • 11
  • 36

2 Answers2

4

Use the regex \b\w+\b to extract words and then reformat like this:

<?php

$strings = [", One ",
    ", One , Two",
    "One, Two ",
    " One,Two, ",
    " ,Two ,Three ",
    ", Two ,Three, Twenty Five, Six"];
foreach($strings as &$str)
{
    preg_match_all('/\b[\w\s]+\b/',$str,$matches);
    $neat = '';
    foreach($matches[0] as $word)
    {
        $neat .= $word.', ';
    }
    $neat = rtrim($neat,', ');
    $str = $neat;
}
print_r($strings);

?>

Output:

Array
(
    [0] => One
    [1] => One, Two
    [2] => One, Two
    [3] => One, Two
    [4] => Two, Three
    [5] => Two, Three, Twenty Five, Six
)
Pruthvi Raj
  • 3,016
  • 2
  • 22
  • 36
  • OP said he had multiple strings, not a single string containing all those, so I made an array – Pruthvi Raj Jun 28 '15 at 06:49
  • Nice approach, but it does not not work if there's two words in string e.g. `, Two ,Three, Twenty Five, Six ` it returns , `Two, Three, Twenty, Five, Six` – Roman Toasov Jun 28 '15 at 06:50
  • @RomanToasov , It's returning what exactly OP asked, what's wrong with it, if so what should have been correct ouput ? :) – Pruthvi Raj Jun 28 '15 at 06:52
  • Correct would be `Two, Three, Twenty Five, Six ` note `Twenty Five ` without coma. I did not think i need to put that in example string as i was just asking how to edit beginning and end parts of string. – Roman Toasov Jun 28 '15 at 06:54
  • @RomanToasov , please add it in question , I'll update my answer :) – Pruthvi Raj Jun 28 '15 at 06:55
0

Since you want to mutate the input strings into consistent comma+space delimited strings, there is no reason to form temporary arrays -- especially if you are open to a regular expression technique.

  1. trim all spaces and commas from the front and back of the input string, then
  2. replace one-or-more comma or space characters with your standardized comma+space glue.

Code: (Demo)

$tests = [
    ", One ,,  ,,",
    ", Two , Three",
    "Four, Five ",
    " Six,Seven, ",
    " ,Eight ,Nine , , , , Ten ,",
];

foreach ($tests as $test) {
    var_export(
        preg_replace('/[, ]+/', ', ', trim($test, ', '))
    );
    echo "\n";
}

Output:

'One'
'Two, Three'
'Four, Five'
'Six, Seven'
'Eight, Nine, Ten'

If you require at least one comma as a delimiting sequence in your input strings, then this adjusted regex will do: /[, ]*,[, ]*/. Demo: https://3v4l.org/HeCFp This will preserve spaces within a value.

",,Eleventy Twelve , , Thrirteen " -> "Eleventy Twelve, Thrirteen"


If you want to remove "junk characters" at the start and end of your input string, just add the !@#$%^&*( characters to the "character mask" parameter of trim() --> , !@#$%^&*(.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136