1

String : ID#6 Item name

I want to remove any ID#1 or ID#18 & keep the word after.

$string = str_replace('ID#', '', $string);
$string = substr(strstr("$string"," "), 1);

Is there a more elegant solution?

5
  • 2
    $str = 'ID#6 Item name'; echo preg_replace ( '/ID#\d+/' , '' , $str ) Commented Nov 16, 2015 at 23:38
  • You'll need to use a regex. str_replace is for static content being replaced. Is it always a number after ID#? Commented Nov 16, 2015 at 23:43
  • @bassxzero +1 @chris85 yes.. always a number after ID# Commented Nov 16, 2015 at 23:52
  • Than @bassxzero comment should work for you. You can test regexs and see what they are doing on various sites for example regex101.com. Commented Nov 16, 2015 at 23:54
  • is the space after the number always there?? Commented Nov 17, 2015 at 0:03

1 Answer 1

1

This might look a bit weird, but its a one liner

$test = "ID#6123234235345asdasdasdasd";
var_dump(ltrim($test, "ID#0..9")); // string(12) "asdasdasdasd"
Sign up to request clarification or add additional context in comments.

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.