简体   繁体   English

PHP Regex删除字符后的所有内容

[英]PHP Regex to remove everything after a character

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. 我需要一个PHP正则表达式来删除冒号之后的所有内容。 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 fooanotherfoo

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? 发布此后, explode()足够可靠地工作? Something like 就像是

$pieces = explode(':', 'foo:bar') 
$newstring = $pieces[0];

explode would do what you're asking for, but you can make it one step by using current . explode您的要求,但是您可以通过使用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). 我不会在这里使用正则表达式(在幕后进行一些相对简单的操作),也不会将strpossubstr一起使用(因为这实际上会遍历字符串两次)。 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 explode a 1 Gb file. 唯一的例外是,如果您碰巧知道该字符串过长:我不会explode 1 Gb文件。 Instead: 代替:

$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不太容易阅读:在current(explode您可以立即看到分隔符,而无需额外的函数调用,并且变量只有一个事件(这使得它不太容易出现人为错误)。读取current(explode为“我正在此字符串之前发生任何事情的第一个事件”,而不是substr ,即“我正在从0位置开始并一直持续到此字符串为止的一个子字符串。”

Your explode solution does the trick. 您的explode解决方案可以解决问题。 If you really want to use regexes for some reason, you could simply do this: 如果您出于某些原因确实想要使用正则表达式,则只需执行以下操作:

$newstring = preg_replace("/(.*?):(.*)/", "$1", $string);

比其他示例更简洁:

current(explode(':', $string));

您可以使用m.buettner编写的RegEx,但他的示例将返回':'之前的所有内容,如果您希望':'之后的所有内容,只需使用$ 2而不是$ 1:

$newstring = preg_replace("/(.*?):(.*)/", "$2", $string);

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;
?>

为什么要使用正则表达式?

list($beforeColon) = explode(':', $string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM