Dynamisch, wenn Sie einen Teil (e) von einem festen Index (n) eines Strings entfernen möchten, verwenden Sie diese Funktion:
/*
* Remove index/indexes from a string, delimited by a space (or something else).
* First, divides the string by delimiter and holds divided parts as an array.
* If $index is an array, it removes all indexes of the divided string;
* otherwise (i.e. has an int value), it removes just that index of the string.
* At last, it joins all string parts together and return it as a string.
* Note: If you want to use this function in PHP5, remove scalar type hints
* (i.e. keywords before function arguments).
*/
function remove_from_str($index, string $str, string $delimiter = " "): string
{
// String parts
$strParts = explode($delimiter, $str);
// Remove indexes from string parts
if (is_array($index))
foreach ($index as $i)
$strParts[(int)$i] = null;
else
$strParts[(int)$index] = null;
// Remove null values, to prevent duplicating delimiter
$strParts = array_filter($strParts);
// Join all parts together and return it
return implode($delimiter, $strParts);
}
Für Ihren Zweck:
remove_from_str(1, "REGISTER 11223344 here");
// Output: REGISTER here
Eine seiner Nutzungen ist die Ausführung von Befehlen, deren Struktur Sie kennen.
Welche Programmiersprache ist das? –
sagt PHP in der Post. –
@siddharth - welche Ausgabe möchten Sie? –