删除字符串中从第二个出现的连字符开始的所有字符

发布于 2024-11-16 00:09:34 字数 234 浏览 5 评论 0原文

如何在字符 - 第二次出现后删除字符串中的所有内容?

例如:今天是 - 星期五,明天是 - 星期六

我希望将 星期六 连同最后一个连字符一起删除:- 星期六

我只能似乎在第一个 - 之后删除所有内容。

预期结果是:今天是 - 星期五,明天是

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一场春暖 2024-11-23 00:09:34

编辑:请注意,正如下面@mickmackusa 所指出的,如果连字符少于两个,这将返回一个空字符串。结束编辑。

使用 strpos 查找第一个出现的位置,然后再次使用它来查找指向的点结束使用带有先前值的偏移选项。然后使用 substr

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

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));
浅浅淡淡 2024-11-23 00:09:34

一些爆炸怎么样:

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

How about some explosions:

$parts = explode( '-', "Today is - Friday and tomorrow is - Saturday" );
echo $parts[0].'-'.$parts[1];
夜无邪 2024-11-23 00:09:34

另一种使用 strtok 的方法:

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

演示

Another way with strtok:

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

DEMO

酷炫老祖宗 2024-11-23 00:09:34

对于其他有同样问题的人;我使用了这种易于调整的紧凑解决方案。

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

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)
拿命拼未来 2024-11-23 00:09:34

您可以使用explode() 在每次出现“-”时分割字符串。 EG:

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

会给你留下:

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

因此,你想要的位将是中间带有“-”的前两项,因此我们可以从数组中弹出最后一个元素并加入其余元素:

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

这给出:

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

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";
裸钻 2024-11-23 00:09:34

我发现正则表达式模式的控制和简洁对于这项任务来说是最有吸引力的。

匹配零个或多个非连字符,然后匹配一个连字符(N 次;本例中为 2 次),在最新的连字符匹配之前用 \K 重置完整字符串匹配,然后匹配其余字符。

如果字符串中的连字符少于 N 个,则此方法不会删除任何字符。

代码:(Demo)

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

如果您想修剪第二个连字符后的尾随空格,您可以将其添加到模式中而不是对返回的字符串调用 rtrim() 。 (演示

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

我个人不喜欢进行三个函数调用来生成临时数组的想法,然后只保留前两个元素,然后将数组重新连接为连字符分隔的字符串,但如果您非常讨厌正则表达式而想要这种方法,则应该将爆炸次数限制为 N+1,以便不会出现不必要的元素创建的。 (演示

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

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))
);
一场信仰旅途 2024-11-23 00:09:34
 $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

 $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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文