从字符串中删除前两个单词

发布于 2024-07-30 18:02:28 字数 228 浏览 2 评论 0原文

我有一个字符串:

$string = "R 124 This is my message";

有时,该字符串可能会发生变化,例如:

$string = "R 1345255 This is another message";

使用 PHP,删除前两个“单词”(例如,最初的“R”,然后是后续数字)的最佳方法是什么?

I have a string:

$string = "R 124 This is my message";

At times, the string may change, such as:

$string = "R 1345255 This is another message";

Using PHP, what's the best way to remove the first two "words" (e.g., the initial "R" and then the subsequent numbers)?

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

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

发布评论

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

评论(5

白馒头 2024-08-06 18:02:28
$string = explode (' ', $string, 3);
$string = $string[2];

一定比正则表达式快得多。

$string = explode (' ', $string, 3);
$string = $string[2];

Must be much faster than regexes.

傲鸠 2024-08-06 18:02:28

一种方法是使用 explodepreg_split (取决于单词分隔符的复杂性:它们总是一个空格吗? )

例如:

$string = "R 124 This is my message";
$words = explode(' ', $string);
var_dump($words);

你会得到一个像这样的数组:

array
  0 => string 'R' (length=1)
  1 => string '124' (length=3)
  2 => string 'This' (length=4)
  3 => string 'is' (length=2)
  4 => string 'my' (length=2)
  5 => string 'message' (length=7)

然后,使用 array_slice ,你只保留你想要的单词(而不是前两个):

$to_keep = array_slice($words, 2);
var_dump($to_keep);

这给出了:

array
  0 => string 'This' (length=4)
  1 => string 'is' (length=2)
  2 => string 'my' (length=2)
  3 => string 'message' (length=7)

最后,你将各个部分放在一起:

$final_string = implode(' ', $to_keep);
var_dump($final_string);

这给出了......

string 'This is my message' (length=18)

并且,如果有必要,它允许你做几个在将单词重新组合在一起之前对单词进行处理:-)

实际上,这就是您可能选择该解决方案的原因,该解决方案比仅使用 explode 和/或 preg_split ^^

One way would be to explode the string in "words", using explode or preg_split (depending on the complexity of the words separators : are they always one space ? )

For instance :

$string = "R 124 This is my message";
$words = explode(' ', $string);
var_dump($words);

You'd get an array like this one :

array
  0 => string 'R' (length=1)
  1 => string '124' (length=3)
  2 => string 'This' (length=4)
  3 => string 'is' (length=2)
  4 => string 'my' (length=2)
  5 => string 'message' (length=7)

Then, with array_slice, you keep only the words you want (not the first two ones) :

$to_keep = array_slice($words, 2);
var_dump($to_keep);

Which gives :

array
  0 => string 'This' (length=4)
  1 => string 'is' (length=2)
  2 => string 'my' (length=2)
  3 => string 'message' (length=7)

And, finally, you put the pieces together :

$final_string = implode(' ', $to_keep);
var_dump($final_string);

Which gives...

string 'This is my message' (length=18)

And, if necessary, it allows you to do couple of manipulations on the words before joining them back together :-)

Actually, this is the reason why you might choose that solution, which is a bit longer that using only explode and/or preg_split ^^

庆幸我还是我 2024-08-06 18:02:28

尝试

$result = preg_replace('/^R \\d+ /', '', $string, 1);

或(如果您希望您的空间以更明显的风格书写)

$result = preg_replace('/^R\\x20\\d+\\x20/', '', $string, 1);

try

$result = preg_replace('/^R \\d+ /', '', $string, 1);

or (if you want your spaces to be written in a more visible style)

$result = preg_replace('/^R\\x20\\d+\\x20/', '', $string, 1);
酒中人 2024-08-06 18:02:28
$string = preg_replace("/^\\w+\\s\\d+\\s(.*)/", '$1', $string);
$string = preg_replace("/^\\w+\\s\\d+\\s(.*)/", '$1', $string);
不甘平庸 2024-08-06 18:02:28
$string = preg_replace('/^R\s+\d+\s*/', '', $string);
$string = preg_replace('/^R\s+\d+\s*/', '', $string);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文