2

I have string with following structure:

AAA-BBB-CCC or 
BBB-CCC or
CCC-DDD

I want to remove first part from these strings.

Result

BBB-CCC
CCC
DDD

Can I do this without "explode" ?

Thanks in advance !

4
  • 1
    What exactly is the rule here? Do you have a reason not to use explode()? Commented Jul 10, 2012 at 6:50
  • use preg_replace Commented Jul 10, 2012 at 6:50
  • You can use Regular Expression Matching to do this. Commented Jul 10, 2012 at 6:51
  • Possible duplicate of Remove a string from the beginning of a string Commented May 17, 2016 at 19:22

6 Answers 6

4

You may use something like this:

$str = "AAA-BBB-CCC";
$str2 = explode("-", $str);
array_shift($str2);
$str = implode("-", $str2);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code if you have always 4 character to delete

$string= substr( $your_string, 4 );

Comments

1

If your string is not having defined length, you could use regex.

$str = "AAA-BBB-CCC";
preg_match('/^.+?-(.+)/', $str, $results);
var_dump($results);

1 Comment

or preg_replace('/^.+?-(.+)/', '$1', $str);
1
$str = "
AAA-BBB-CCC
BBB-CCC
CCC-DDD
";

$str = preg_replace('/^([A-Z]{3}\-)/m', '', $str);
var_dump($str);

This works. Just modify the regex slightly to fit your format if it goes beyond your example string.

Comments

1

yes, just use strpos and substr like this:

$string = substr($string, strpos($string, '-'));

this cuts at the first - - if the parts are always the same length, it's even easier:

$string = substr($string, 4);

Comments

0

If the first part is always four letters, you can use this:

$newString = substr($str, 4);

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.