Search code examples
phpregextrim

PHP preg_replace help trim special characters


I tried to search around but couldn't find anything useful. I need to trim special characters from beginning and end of a string and identify if the remaining portion is a number.

For example

(5)
[[12]]
{3}
#!8(#
!255=
/879/

I need a preg_match expression for it. The regular expression should ignore the string if any alphabets come in between.


Solution

  • $string="yourstring";
    $new_string=preg_replace('/[^A-Za-z0-9]/', '', $string);
    if(is_numeric($new_string){
        echo "number";
    } else {
        echo "string";
    }