Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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 !
explode()
You may use something like this:
$str = "AAA-BBB-CCC"; $str2 = explode("-", $str); array_shift($str2); $str = implode("-", $str2);
Add a comment
Try this code if you have always 4 character to delete
$string= substr( $your_string, 4 );
If your string is not having defined length, you could use regex.
$str = "AAA-BBB-CCC"; preg_match('/^.+?-(.+)/', $str, $results); var_dump($results);
$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.
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);
If the first part is always four letters, you can use this:
$newString = substr($str, 4);
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
explode()?