How can I remove part of a string?
Example string: "REGISTER 11223344 here"
How can I remove "11223344"
from the above example string?
The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.
You can use strstr to do this. Show activity on this post. The explode is in fact a better answer, as the question was about removing the text before the string.
Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a 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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With