I need to remove all characters from any string before the occurrence of this inside the string: Show
Not sure how I can do this.
Charles 50.3k13 gold badges101 silver badges141 bronze badges asked Oct 18, 2011 at 5:33
user547794user547794 13.8k35 gold badges100 silver badges150 bronze badges 2
You can use strstr to do this.
answered Oct 18, 2011 at 5:36
1 Considering
Either you can use
Or
answered Oct 18, 2011 at 5:39
WazyWazy 8,71010 gold badges54 silver badges97 bronze badges 3 I use this functions
answered Oct 18, 2011 at 5:45
2 You can use substring and strpos to accomplish this goal. You could also use a regular expression to pattern match only what you want. Your mileage may vary on which of these approaches makes more sense. answered Oct 18, 2011 at 5:37
thedayturnsthedayturns 8,7164 gold badges32 silver badges38 bronze badges Not the answer you're looking for? Browse other questions tagged php string or ask your own question.View Discussion Improve Article Save Article View Discussion Improve Article Save Article The substr() and strpos() function is used to remove portion of string after certain
character. Syntax: strpos( original_string, search_string, start_pos ) Return Value: This function returns an integer value which represents the index of original_str where the string search_str first occurs. substr() function: The substr() function is an inbuilt function in PHP is used to extract a specific part of string. Syntax: substr( string_name, start_position, string_length_to_cut ) Return Value: Returns the extracted part of the string if successful otherwise FALSE or an empty string on failure. Below examples use substr() and strpos() function to remove portion of string after certain character. Example 1:
Output: GeeksForGeeks is the best platform. GeeksForGeeks Example 2:
Output: computer science portal for geeks computer science portal How can I remove part of a string after a specific character in PHP?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.
How do I remove all characters from a string before a specific character?To remove everything before the first occurrence of the character '-' in a string, pass the character '-' as a separator in the partition() function. Then assign the part after the separator to the original string variable. It will give an effect that we have deleted everything before the character '-' in a string.
How do I remove all characters before a specific character in PHP?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.
How do I remove all characters from a string after a specific character?Using 'str.
replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
|