samirah
samirah

Reputation: 199

Strip all characters in a string starting from the second occurring hyphen

How can I strip everything in a string after the character - has occurred for the second time?

For example: Today is - Friday and tomorrow is - Saturday

I want Saturday to be removed along with the last hyphen: - Saturday

I can only seem to get everything to be removed after the first -.

The expected result is: Today is - Friday and tomorrow is

Upvotes: 14

Views: 19588

Answers (7)

Rasika
Rasika

Reputation: 1998

Edit: Please note as noted by @mickmackusa below, this will return an empty string if there are less than two hyphens. End Edit.

Use strpos to find the first occurrence and use it again to find the point to end using the offset option with the value from previous. Then use substr.

$newstr = substr($str, 0, strpos($str, '-', strpos($str, '-')+1));

Upvotes: 15

mickmackusa
mickmackusa

Reputation: 47764

I find the control and brevity of a regex pattern to be most attractive for this task.

Match zero or more non-hyphens, then a hyphen (N amount of times; 2 in this case), resetting the full string match with \K before the latest hyphen matched, then match the remaining characters.

This approach will not remove any characters if there are less then N hyphens in the string.

Code: (Demo)

$string = "Today is - Friday and tomorrow is - Saturday";
var_export(
    preg_replace('/(?:[^-]*\K-){2}.*/', '', $string)
);

If you want to trim trailing spaces after the second hyphen, you can add that to the pattern instead of calling rtrim() on the returned string. (Demo)

var_export(
    preg_replace('/(?:[^-]*?\K\s*-){2}.*/', '', $string)
);

I don't personally enjoy the idea of making three function calls to generate a temporary array, then only keep upto the first two elements, then re-joining the array to be a hyphen-delimited string, but if you hate regex so much to want that approach you should limit the number of explosions to be N+1 so that no unnecessary elements are created. (Demo)

var_export(
    implode('-', array_slice(explode('-', $string, 3), 0, 2))
);

Upvotes: 0

SebHallin
SebHallin

Reputation: 891

For others with the same problem; I used this compact solution that is easy to adjust.

$str = 'Today is - Friday and tomorrow is - Saturday';

$sliceWith = "-"; // character to split by
$beginWith = 0; // 1 removes before first match, 0 will not
$splitAfter = 2; // number of matches to keep

$result = implode($sliceWith, array_slice(explode($sliceWith, $str), $beginWith, $splitAfter));

echo $result; // You might want to use trim($result)

Upvotes: 3

Jack Franklin
Jack Franklin

Reputation: 3765

You could use explode() to split the string at each occurrence of "-". EG:

$str = "Today is - Friday and tomorrow is - Saturday"
$parts = explode(" - ", $str);

Would leave you with:

$parts = ["Today is", "Friday and tomorrow is", "Saturday"]

And as such the bit you want is going to be the first two items with a "-" in the middle, so we can pop the last element from the array and join the rest:

array_pop($parts);
$result = implode(" - ", $parts);

Which gives:

$result == "Today is - Friday and tomorrow is";

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816232

Another way with strtok:

$newStr = strtok($str, '-') . '-' . strtok('-');

DEMO

Upvotes: 6

ben
ben

Reputation: 1936

 $string = "Today is - Friday and tomorrow is - Saturday";
 $first_dash = strpos($string, '-');
 $second_dash = strpos($string, '-', $first_dash+1);
 $new_string = substr($string, $second_dash+1);

strpos

substr

Upvotes: -1

Bailey Parker
Bailey Parker

Reputation: 15905

How about some explosions:

$parts = explode( '-', "Today is - Friday and tomorrow is - Saturday" );
echo $parts[0].'-'.$parts[1];

Upvotes: 14

Related Questions