Dynamiquement, si vous souhaitez supprimer une partie (s) à partir d'un indice fixe (s) d'une chaîne, utilisez cette fonction:
/*
* 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);
}
Pour votre but:
remove_from_str(1, "REGISTER 11223344 here");
// Output: REGISTER here
Un de ses usages est d'exécuter des commandes dont vous connaissez la structure.
De quel langage de programmation s'agit-il? –
dit PHP dans le post. –
@siddharth - quelle sortie voulez-vous? –