How to remove letter from string in 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!

Overflowh

1,0936 gold badges18 silver badges40 bronze badges

asked Nov 5, 2008 at 6:34

1

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().

answered Nov 5, 2008 at 6:49

0

$str = preg_replace('/\?\//', '?', $str);

Edit: See CMS' answer. It's late, I should know better.

answered Nov 5, 2008 at 6:37

eyelidlessnesseyelidlessness

61.2k11 gold badges89 silver badges93 bronze badges

2

While a regexp would suit here just fine, I'll present you with an alternative method. It might be a tad faster than the equivalent regexp, but life's all about choices (...or something).

$length = strlen($urlString);
for ($i=0; $i<$length; i++) {
  if ($urlString[$i] === '?') {
    $urlString[$i+1] = '';
    break;
  }
}

Weird, I know.

answered Nov 5, 2008 at 6:41

Henrik PaulHenrik Paul

66k31 gold badges84 silver badges96 bronze badges

4

$splitPos = strpos($url, "?/");
if ($splitPos !== false) {
    $url = substr($url, 0, $splitPos) . "?" . substr($url, $splitPos + 2);
}

answered Nov 5, 2008 at 6:49

nickfnickf

525k198 gold badges640 silver badges720 bronze badges

In this article, we will see how to remove special characters from strings in PHP, along with understanding their implementation through the examples. We have given a string and we need to remove special characters from string str in PHP, for this, we have the following methods in PHP:

Using str_replace() Method: The str_replace() methodis used to remove all the special characters from the given string str by replacing these characters with the white space (” “).

Syntax:

str_replace( $searchVal, $replaceVal, $subjectVal, $count )

Example: This example illustrates the use of the str_replace() function to remove the special characters from the string.

PHP

  function RemoveSpecialChar($str) {

      $res = str_replace( array( '\'', '"',

      ',' , ';', '<', '>' ), ' ', $str);

      return $res;

      }

  $str = "Example,to removeSpecial'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

Using str_ireplace() Method: The str_ireplace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “). The difference between str_replace and str_ireplace is that str_ireplace is case-insensitive.

Syntax:

str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )

Example: This example illustrates the use of the str_ireplace() method to remove all the special characters from the string.

PHP

  function RemoveSpecialChar($str){

      $res = str_ireplace( array( '\'', '"',

      ',' , ';', '<', '>' ), ' ', $str);

      return $res;

      }

  $str = "Example,to removeSpecial'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

Using preg_replace() Method: The preg_replace() method is used to perform a regular expression for search and replace the content.

Syntax:

preg_replace( $pattern, $replacement, $subject, $limit, $count )

Example: This example illustrates the use of the preg_replace() method where the particular or all the matched pattern found in the input string will be replaced with the substring or white space.

PHP

  function RemoveSpecialChar($str){

      $res = preg_replace('/[^a-zA-Z0-9_ -]/s',' ',$str);

      return $res;

  }

  $str = "Example,to removeSpecial'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How remove all letters from a string in PHP?

Strip all characters but letters, numbers, and whitespace Finally, if you want to strip all characters from your string other than letters, numbers, and whitespace, this regular expression will do the trick: $res = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);

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.

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.

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.