preg_replace除字符串开头之外的所有数字和星号字符

Need to replace all characters in a string except for any that are at the start of a string (or part of a word).

For example input:

MSFT *<E07004QY6W>
WOOLWORTHS W1157
GOOGLE*ADWS7924436927
COLES 0829
ROBLOX.COM 888-858-25
7-ELEVEN 2179
COLES EXPRESS 1896

result should be:

MSFT
WOOLWORTHS
GOOGLE
COLES
ROBLOX.COM
7-ELEVEN
COLES EXPRESS

Can php preg_replace achieve this?

Tried so far:

  • '/\d+/g' - but it removes ALL digits and nothing else
  • '/(*|\d+$)/' - but this doesn't quite work on some, results in "ROBLOX.COM 888-858-"

Not sure this will work for other edge cases, but you can try with this replacement:

$txt = preg_replace('~^[^*\s]+(?: \pL+(?!\S))*\K.*~m', '', $txt);

demo

Explanations:

^[^*\s]+ takes all that isn't a space or an asterisk at the start of the line.
(?: \pL+(?!\S))* and eventually group of letters separated by spaces.
\K removes all previous matched characters from the match result.
.* takes all the remaining characters that will be replaced.

$in = array(
    'MSFT *<E07004QY6W>',
    'WOOLWORTHS W1157',
    'GOOGLE*ADWS7924436927',
    'COLES 0829',
    'ROBLOX.COM 888-858-25',
    '7-ELEVEN 2179',
    'COLES EXPRESS 1896',
);

foreach ($in as $str) {
    echo preg_replace('/[\h*]+[^\h*]+$/', '', $str),"
";
}

Output:

MSFT
WOOLWORTHS
GOOGLE
COLES
ROBLOX.COM
7-ELEVEN
COLES EXPRESS