So I've seen a couple articles that go a little too deep, so I'm not sure what to remove from the regex statements they make.
所以我看到一些文章有点太深,所以我不确定从他们制作的正则表达式语句中删除什么。
I've basically got this
我基本上得到了这个
foo:bar all the way to anotherfoo:bar;seg98y34g.?sdebvw h segvu (anything goes really)
foo:bar一直到另一个foo:bar; seg98y34g。?sdebvw h segvu(真的很棒)
I need a PHP regex to remove EVERYTHING after the colon. the first part can be any length (but it never contains a colon. so in both cases above I'd end up with
我需要一个PHP正则表达式来删除冒号后的所有东西。第一部分可以是任何长度(但它从不包含冒号。所以在上述两种情况下我都会结束
foo and anotherfoo
foo和anotherfoo
after doing something like this horrendous example of psuedo-code
在做了类似psuedo-code这个可怕的例子之后
$string = 'foo:bar';
$newstring = regex_to_remove_everything_after_":"($string);
EDIT
编辑
after posting this, would an explode()
work reliably enough? Something like
在发布之后,explode()的运行是否足够可靠?就像是
$pieces = explode(':', 'foo:bar')
$newstring = $pieces[0];
6 个解决方案
#1
20
explode
would do what you're asking for, but you can make it one step by using current
.
爆炸会做你想要的,但你可以通过使用当前的一步。
$beforeColon = current(explode(':', $string));
I would not use a regex here (that involves some work behind the scenes for a relatively simple action), nor would I use strpos
with substr
(as that would, effectively, be traversing the string twice). Most importantly, this provides the person who reads the code with an immediate, "Ah, yes, that is what the author is trying to do!" instead of, "Wait, what is happening again?"
我不会在这里使用正则表达式(这涉及到一些相对简单的动作在幕后工作),也不会使用带有substr的strpos(因为这将有效地遍历字符串两次)。最重要的是,这为立即阅读代码的人提供了“啊,是的,这就是作者想要做的!”而不是,“等等,再次发生了什么?”
The only exception to that is if you happen to know that the string is excessively long: I would not explode
a 1 Gb file. Instead:
唯一的例外是,如果您碰巧知道字符串过长:我不会爆炸1 Gb文件。代替:
$beforeColon = substr($string, 0, strpos($string,':'));
I also feel substr
isn't quite as easy to read: in current(explode
you can see the delimiter immediately with no extra function calls and there is only one incident of the variable (which makes it less prone to human errors). Basically I read current(explode
as "I am taking the first incident of anything prior to this string" as opposed to substr
, which is "I am getting a substring starting at the 0 position and continuing until this string."
我也觉得substr不太容易阅读:在当前(爆炸你可以立即看到分隔符,没有额外的函数调用,只有一个变量事件(这使得它不容易出现人为错误)。基本上我读取当前(爆炸为“我正在接受此字符串之前的任何事件的第一次事件”而不是substr,这是“我从0位置开始并继续直到此字符串。”
#2
8
Your explode
solution does the trick. If you really want to use regexes for some reason, you could simply do this:
你的爆炸解决方案可以解决问题。如果你真的想出于某种原因使用正则表达式,你可以简单地这样做:
$newstring = preg_replace("/(.*?):(.*)/", "$1", $string);
#3
3
A bit more succinct than other examples:
比其他例子更简洁:
current(explode(':', $string));
#4
2
You can use RegEx that m.buettner wrote, but his example returns everything BEFORE ':', if you want everything after ':' just use $2 instead of $1:
您可以使用m.buettner编写的RegEx,但是他的示例返回BEFORE':'之前的所有内容,如果您想要':'之后的所有内容,只需使用$ 2而不是$ 1:
$newstring = preg_replace("/(.*?):(.*)/", "$2", $string);
#5
0
You could use something like the following. demo: http://codepad.org/bUXKN4el
您可以使用以下内容。演示:http://codepad.org/bUXKN4el
<?php
$s = 'anotherfoo:bar;seg98y34g.?sdebvw h segvu';
$result = array_shift(explode(':', $s));
echo $result;
?>
#6
0
Why do you want to use a regex?
为什么要使用正则表达式?
list($beforeColon) = explode(':', $string);
#1
20
explode
would do what you're asking for, but you can make it one step by using current
.
爆炸会做你想要的,但你可以通过使用当前的一步。
$beforeColon = current(explode(':', $string));
I would not use a regex here (that involves some work behind the scenes for a relatively simple action), nor would I use strpos
with substr
(as that would, effectively, be traversing the string twice). Most importantly, this provides the person who reads the code with an immediate, "Ah, yes, that is what the author is trying to do!" instead of, "Wait, what is happening again?"
我不会在这里使用正则表达式(这涉及到一些相对简单的动作在幕后工作),也不会使用带有substr的strpos(因为这将有效地遍历字符串两次)。最重要的是,这为立即阅读代码的人提供了“啊,是的,这就是作者想要做的!”而不是,“等等,再次发生了什么?”
The only exception to that is if you happen to know that the string is excessively long: I would not explode
a 1 Gb file. Instead:
唯一的例外是,如果您碰巧知道字符串过长:我不会爆炸1 Gb文件。代替:
$beforeColon = substr($string, 0, strpos($string,':'));
I also feel substr
isn't quite as easy to read: in current(explode
you can see the delimiter immediately with no extra function calls and there is only one incident of the variable (which makes it less prone to human errors). Basically I read current(explode
as "I am taking the first incident of anything prior to this string" as opposed to substr
, which is "I am getting a substring starting at the 0 position and continuing until this string."
我也觉得substr不太容易阅读:在当前(爆炸你可以立即看到分隔符,没有额外的函数调用,只有一个变量事件(这使得它不容易出现人为错误)。基本上我读取当前(爆炸为“我正在接受此字符串之前的任何事件的第一次事件”而不是substr,这是“我从0位置开始并继续直到此字符串。”
#2
8
Your explode
solution does the trick. If you really want to use regexes for some reason, you could simply do this:
你的爆炸解决方案可以解决问题。如果你真的想出于某种原因使用正则表达式,你可以简单地这样做:
$newstring = preg_replace("/(.*?):(.*)/", "$1", $string);
#3
3
A bit more succinct than other examples:
比其他例子更简洁:
current(explode(':', $string));
#4
2
You can use RegEx that m.buettner wrote, but his example returns everything BEFORE ':', if you want everything after ':' just use $2 instead of $1:
您可以使用m.buettner编写的RegEx,但是他的示例返回BEFORE':'之前的所有内容,如果您想要':'之后的所有内容,只需使用$ 2而不是$ 1:
$newstring = preg_replace("/(.*?):(.*)/", "$2", $string);
#5
0
You could use something like the following. demo: http://codepad.org/bUXKN4el
您可以使用以下内容。演示:http://codepad.org/bUXKN4el
<?php
$s = 'anotherfoo:bar;seg98y34g.?sdebvw h segvu';
$result = array_shift(explode(':', $s));
echo $result;
?>
#6
0
Why do you want to use a regex?
为什么要使用正则表达式?
list($beforeColon) = explode(':', $string);