dido
dido

Reputation: 2361

PHP - Remove part of string?

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 !

Upvotes: 2

Views: 5559

Answers (6)

oezi
oezi

Reputation: 51817

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);

Upvotes: 1

MrSil
MrSil

Reputation: 618

You may use something like this:

$str = "AAA-BBB-CCC";
$str2 = explode("-", $str);
array_shift($str2);
$str = implode("-", $str2);

Upvotes: 4

Kevin
Kevin

Reputation: 1143

$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.

Upvotes: 1

Jithin
Jithin

Reputation: 2604

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

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

Upvotes: 1

user1427811
user1427811

Reputation:

Try this code if you have always 4 character to delete

$string= substr( $your_string, 4 );

Upvotes: 1

jeff
jeff

Reputation: 8348

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

$newString = substr($str, 4);

Upvotes: 0

Related Questions