Bits Please
Bits Please

Reputation: 897

Remove numbers in string php

I have a particular string

$email = "[email protected]";

I want sanitize and remove +1 from the string, it can be any number +1 or +99 as well, so the final string can be

$email = "[email protected]";

I dont know what to do, I tried creating this method but it gives me this output.

$email = jaymingmail.com;

Below is my function:

public function delete_all_between($beginning, $end, $string) {
      $beginningPos = strpos($string, $beginning);
      $endPos = strpos($string, $end);
      if ($beginningPos === false || $endPos === false) {
        return $string;
      }

      $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);

      return $this->delete_all_between($beginning, $end, str_replace($textToDelete, '', $string)); // recursion to ensure all occurrences are replaced
    }


$out = $this->delete_all_between('+', '@', $email);

Can anyone help me out where I am going wrong.

I need to remove +1 or any number after + symbol.

Upvotes: 2

Views: 516

Answers (1)

Andreas
Andreas

Reputation: 23958

You can do it with one line using preg_replace

This finds a literal + and any digits after and deletes them.

echo preg_replace("/(\+\d+)/", "", "[email protected]");

Output: [email protected]

Upvotes: 7

Related Questions