How to remove portion of a string after a certain 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. Function treats upper-case and lower-case characters differently.
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.
Here are few examples.
Below examples use substr() and strpos() function to remove portion of string after certain character.
Example 1:
<?php // Declare a variable and initialize it $variable = "GeeksForGeeks is the best platform."; // Display value of variable echo $variable; echo "\n"; // Use substr() and strpos() function to remove // portion of string after certain character $variable = substr($variable, 0, strpos($variable, "is")); // Display value of variable echo $variable; ?> |
GeeksForGeeks is the best platform. GeeksForGeeks
Example 2:
<?php // Declare a variable and initialize it $variable = "computer science portal for geeks"; // Display value of variable echo $variable; echo "\n"; // Use substr() and strpos() function to remove // portion of string after certain character // and display it echo substr($variable, 0, strpos($variable, "for")); ?> |
computer science portal for geeks computer science portal
Recommended Posts:
- How to remove portion of a string after certain character in JavaScript ?
- How to remove the first character of string in PHP?
- How to remove a character from string in JavaScript ?
- How to get decimal portion of a number using JavaScript ?
- How to get the last character of a string in JavaScript?
- Remove new lines from string in PHP
- How to remove extension from string in PHP?
- Delete first character of a string in JavaScript
- How to get character array from string in JavaScript?
- How to remove all non-printable characters in a string in PHP?
- How to remove line breaks from the string in PHP?
- How to remove text from a string in JavaScript?
- Removing occurrences of a specific character from end of a string in PHP
- Difference between String and Character array in Java
- Convert a String to Character array in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



