Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP removing a character in a string

Tags:

string

php

My php is weak and I'm trying to change this string:

http://www.example.com/backend.php?/c=crud&m=index&t=care                                    ^ 

to be:

http://www.example.com/backend.php?c=crud&m=index&t=care                                   ^ 

removing the / after the backend.php?. Any ideas on the best way to do this?

Thanks!

like image 469
Zero Cool Avatar asked Nov 05 '08 06:11

Zero Cool


People also ask

How can I remove one character from a string in PHP?

Explanation: In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known. $str = "geeks" ; // Or we can write ltrim($str, $str[0]);

How do I remove a specific character from a string?

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.

How do I remove a word from a string in PHP?

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.

Why TRIM () function is used in PHP?

Definition and Usage. The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.


1 Answers

I think that it's better to use simply str_replace, like the manual says:

If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().

<? $badUrl = "http://www.site.com/backend.php?/c=crud&m=index&t=care"; $goodUrl = str_replace('?/', '?', $badUrl); 
like image 113
Christian C. Salvadó Avatar answered Oct 19 '22 06:10

Christian C. Salvadó