0

I need to remove a number only from a specific string. In particular:

$item = preg_replace('/\d+/u', '', $item);

but in this way it replaces all numbers from all strings. I instead need to remove only number after string 'team'.

How can I do this?

team2567 = team;
season1617 = season1617;

Thanks a lot!

4 Answers 4

3

make it like

$item = preg_replace('/team\d+/u', 'team', $item);
Sign up to request clarification or add additional context in comments.

Comments

2

Use Positive Lookbehind

$item = preg_replace('/(?<=team)\d+/u', '', $item);

Comments

0
 $str = 'In My Cart : 11 12 items';
 preg_match_all('!\d+!', $str, $matches);
 print_r($matches);

Comments

0

Do something like

$item = preg_replace('/team\d+/u', 'team', $item);

or with capturing group

$item = preg_replace('/(team)\d+/u', '$1', $item);

or with positive lookbehind

$item = preg_replace('/(?<=team)\d+/u', '', $item);

1 Comment

Or preg_replace('/(team)\d+/', '$1', $item)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.