简体   繁体   English

修剪某个子字符串之前的所有字符(php)

[英]Trim all characters before a certain substring (php)

I have this string: 我有这个字符串:

"(data I don't care about) (invariable substring) (data I care about)"

How do I trim all the data I don't care about, knowing the invariable substring? 知道不变的子串,如何修剪我不关心的所有数据?

This should suffice: 这应该足够了:

substr($string, (int)strpos("invariablestring", $string));

But will leave the invariablestring untouched. 但是会保持invariablestring不受影响。 If you want to remove that string, just add it's length to the value strpos() returned. 如果要删除该字符串,只需将其长度添加到返回的值strpos()即可。

最简单的我发现:

list(, $data_i_care_about) = explode('(invariable substring)', $all_the_data, 2)

You could get the offset and only take the substring from that offset on: 您可以获取偏移量并仅从该偏移量中获取子字符串:

if (($pos = strpos($str, $substr)) !== false) {
    $str = substr($str, $pos);
}

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

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