php 得到一切后的词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11290279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get everything after word
提问by halliewuud
Take this Lorem Ipsum text:
以这个 Lorem Ipsum 文本为例:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla felis diam, mattis id elementum eget, ullamcorper et purus.
Lorem ipsum dolor 坐 amet,consectetur adipiscing 精英。Nulla felis diam、mattis id elementum eget、ullamcorper et purus。
How can I with PHP and regex get everything that comes after Nulla?
我怎样才能使用 PHP 和正则表达式获得之后的所有内容Nulla?
回答by PEM
Hmm you don't want to use some simple things like :
嗯,你不想使用一些简单的东西,比如:
$str = substr($lorem, strpos($lorem, 'Nulla'));
if you do not want to look for Nulla, but also for 'null' you might consider using stripos instead of strpos... This code will include Nulla in the returned value. If you want to exclude Nulla, you might want to add it's lentgh to the strpos value i.e
如果您不想查找 Nulla,但也不想查找 'null',您可以考虑使用 stripos 而不是 strpos...此代码将在返回值中包含 Nulla。如果你想排除 Nulla,你可能想将它的 lentgh 添加到 strpos 值,即
$str = substr($lorem, strpos($lorem, 'Nulla') + 5);
At last, if you need to have something a bit more generic, and as suggested @Francis :
最后,如果你需要一些更通用的东西,正如@Francis 所建议的那样:
$needle = 'Nulla';
$str = substr($lorem, strpos($lorem, $needle) + strlen($needle));
Honestly regexp are overkill for something like this...
老实说,正则表达式对于这样的事情来说太过分了......
回答by Igor Chubin
/Nulla(.*)/
Now you have all the text after Nulla in $1
现在你可以在 $1 中获得 Nulla 之后的所有文本
回答by Nikola K.
Try this:
尝试这个:
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla felis diam, mattis id elementum eget, ullamcorper et purus.";
$prefix = "Nulla";
$index = strpos($string, $prefix) + strlen($prefix);
$result = substr($string, $index);
回答by TigerTiger
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla felis diam, mattis id elementum eget, ullamcorper et purus.';
preg_match('/Nulla(.*)/',$string, $matches);
print_r($matches);

