How can I remove part of a string in PHP? [closed]
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this questionHow can I remove part of a string?
Example string: "REGISTER 11223344 here"
H开发者_JAVA百科ow can I remove "11223344"
from the above example string?
If you're specifically targetting "11223344", then use str_replace
:
// str_replace($search, $replace, $subject)
echo str_replace("11223344", "","REGISTER 11223344 here");
You can use str_replace(), which is defined as:
str_replace($search, $replace, $subject)
So you could write the code as:
$subject = 'REGISTER 11223344 here' ;
$search = '11223344' ;
$trimmed = str_replace($search, '', $subject) ;
echo $trimmed ;
If you need better matching via regular expressions you can use preg_replace().
Assuming 11223344 is not constant:
$string="REGISTER 11223344 here";
$s = explode(" ", $string);
unset($s[1]);
$s = implode(" ", $s);
print "$s\n";
str_replace(find, replace, string, count)
- find Required. Specifies the value to find
- replace Required. Specifies the value to replace the value in find
- string Required. Specifies the string to be searched
- count Optional. A variable that counts the number of replacements
As per OP example:
$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"
When you need rule-based matching, you need to use a regular expression:
$string = "REGISTER 11223344 here";
preg_match("/(\d+)/", $string, $match);
$number = $match[1];
That will match the first set of numbers, so if you need to be more specific, try:
$string = "REGISTER 11223344 here";
preg_match("/REGISTER (\d+) here/", $string, $match);
$number = $match[1];
substr() is a built-in PHP function which returns part of a string. The function substr() will take a string as input, the index form where you want the string to be trimmed, and an optional parameter is the length of the substring. You can see proper documentation and example code on substr.
Note: index for a string starts with 0.
Dynamically, if you want to remove (a) part(s) from (a) fixed index(es) of a string, use this function:
/**
* 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);
}
For your purpose:
remove_from_str("REGISTER 11223344 here", 1); // Output: REGISTER here
One of its usages is to execute command-like strings, which you know their structures.
The following snippet will print "REGISTER here"
$string = "REGISTER 11223344 here";
$result = preg_replace(
array('/(\d+)/'),
array(''),
$string
);
print_r($result);
The preg_replace() API usage is as given below.
$result = preg_replace(
array('/pattern1/', '/pattern2/'),
array('replace1', 'replace2'),
$input_string
);
Do the following
$string = 'REGISTER 11223344 here';
$content = preg_replace('/REGISTER(.*)here/','',$string);
This would return "REGISTERhere"
or
$string = 'REGISTER 11223344 here';
$content = preg_replace('/REGISTER (.*) here/','',$string);
This would return "REGISTER here"
精彩评论