1

I want to remove brackets and number from beginning of string but problem is brackets and number is coming only specific string not all.

for example following is my string.

1) [4] Mustangs 8u
2) Pool a First Place
3) Team slect
4) [3] In pruduct

so above you can see only 1 and 4 string have number with brackets at beginning of string so i want to only remove that if that found in string.

I write following code but is not work.

<?php

foreach ($grouped as $round_number => $group) {

        $team_1_name = $group->team_1_name;
        $new_str = preg_replace('/^([0-9]* \w+ )?(.*)$/', '$2', $team_1_name);

        $date = date ('F d, Y g:iA', $unix_time);

    }

?>
2
  • You mean string [4] Mustangs 8u must be Mustangs 8u after code? Commented Jul 5, 2018 at 6:30
  • Yes, also sting ' [3] In pruduct' there are many sting in loop but i found some with number and brackets so i only remove those stings in loop. Commented Jul 5, 2018 at 6:31

3 Answers 3

3

Try regular expression /^(\[[0-9]\]?\s?)/ as:

$new_str = preg_replace('/^(\[[0-9]\]?\s?)/', '$2', $team_1_name);

For reference: regexr

Sign up to request clarification or add additional context in comments.

Comments

2

In case your numbers are multi digit (i.e. '[11] In pruduct')...

echo preg_replace('/^(\[\d*\]?\s?)/', '$2', $team_1_name);

Comments

0

Instead regex you can use ltrim() with a character mask. If your strings never start with a number:

$new_str = ltrim($team_1_name, "0123456789[] ");

else you could check if the first char is a bracket:

$new_str = $team_1_name[0] == '[' ? ltrim($team_1_name, '0123456789[] ') : '';

Comments

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.