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)
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
foo and anotherfoo
after doing something like this horrendous example of psuedo-code
$string = 'foo:bar';
$newstring = regex_to_remove_everything_after_":"($string);
EDIT
after posting this, would an explode() work reliably enough? Something like
$pieces = explode(':', 'foo:bar')
$newstring = $pieces[0];
explodewould do what you’re asking for, but you can make it one step by usingcurrent.I would not use a regex here (that involves some work behind the scenes for a relatively simple action), nor would I use
strposwithsubstr(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?”The only exception to that is if you happen to know that the string is excessively long: I would not
explodea 1 Gb file. Instead:I also feel
substrisn’t quite as easy to read: incurrent(explodeyou 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 readcurrent(explodeas “I am taking the first incident of anything prior to this string” as opposed tosubstr, which is “I am getting a substring starting at the 0 position and continuing until this string.”