/**
* Removes index/indexes from a string, using a delimiter.
*
* @param string $string
* @param int|int[] $index An index, or a list of indexes to be removed from string.
* @param string $delimiter
* @return string
* @todo Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the
* types before each argument) and the return type.
*/
function removeFromString(string $string, $index, string $delimiter = " "): string
{
$stringParts = explode($delimiter, $string);
// Remove indexes from string parts
if (is_array($index)) {
foreach ($index as $i) {
unset($stringParts[(int)($i)]);
}
} else {
unset($stringParts[(int)($index)]);
}
// Join all parts together and return it
return implode($delimiter, $stringParts);
}
为了你的目的:
remove_from_str("REGISTER 11223344 here", 1); // Output: REGISTER here
$Example_string = "REGISTER 11223344 here";
$Example_string_PART_REMOVED = str_replace('11223344', '', $Example_string);
// will leave you with "REGISTER here"
// finally - clean up potential double spaces, beginning spaces or end spaces that may have resulted from removing the unwanted string
$Example_string_COMPLETED = trim(str_replace(' ', ' ', $Example_string_PART_REMOVED));
// trim() will remove any potential leading and trailing spaces - the additional 'str_replace()' will remove any potential double spaces
// will leave you with "REGISTER here"