Change language

Removing occurrences of a specific character from the end of a string in PHP

Using the rtrim() function: this function is a PHP built-in function that removes spaces or other characters (if specified) on the right side of the string.Syntax:
rtrim ($string, $charlist) 
Parameters:This function takes two parameters as above and described below:
  • $string:this is a required parameter that specifies the string to check .
  • $charlist:This is an optional parameter. It specifies which characters should be removed from the string.
Example:This example uses the rtrim() functionto remove & # 39 ;. ’ from the end.  
// Declare a variable and initialize it $string = "GeeksforGeeks is a best platform ....." ;  
// Use rtrim() to crop
// line to the right echo rtrim ( $string , "." );  
?>
Exit:
GeeksforGeeks is a best platform 
Using preg_replace() function: this function is built in PHP function that is used to execute regular expressions to find and replace content.Syntax:
preg_replace ($patt, $replace, $string, $limit, $count) 
Parameters:This function takes five parameters as above and described below:
  • $patt:contains a string element that is used to search for content, and can be a string or an array of strings.
  • $replace:a required parameter that specifies a string or an array of strings for replacements.
  • $string:a string or array with strings to find and replace.
  • $limit:The parameter defines the maxim There are some possible replacements for each template.
  • $count:optional. This variable will be populated with the number of replacements performed.
Example 2: Inthis example uses the function preg_replace()to remove ’ . ’ from the end.  
// Declare a variable and initialize it $string = "GeeksforGeeks is a best platform ....." ;  
// The character to be replaced $regex = "/.+$/" ;  
// Use the preg_replace() function to replace
// character echo preg_replace ( $regex , "" , $string );  
?>
Exit:
GeeksforGeeks is a best platform 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method