我有一个长字符串,其中包含 8 个字符,我需要从中取出。8 个字符总是出现在特定数字之后。
例子:
blahblah1234WMCRCA01其他的这并不重要
除此之外,我需要将变量设置为 1234 之后的 8 个字符,即 WMCRCA01
这应该有助于:
$string = "blahblah1234WMCRCA01therestofthisisntimportant";
$startingString = "1234";
$startingStringAt = strpos($string, $startingString);
if (false === $startingStringAt) {
echo "Could not find the \"$startingString\"";
} else {
$eightCharacters = substr($string, $startingStringAt + 4, 8);
echo "Eight characters found: " . $eightCharacters;
}
这是一个复杂的解决方案,包括在基本字符串中没有出现前面的“1234”字符串的情况。