Ben
Ben

Reputation: 1032

preg_replace all digit and asterisk characters except at start of string

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:

Upvotes: 1

Views: 1186

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

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.

Upvotes: 3

Toto
Toto

Reputation: 91385

$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),"\n";
}

Output:

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

Upvotes: 0

Related Questions