0

I've got the following code:

<?php
    $test = "xxxx..AAA?!";
    echo $test."\n";
    $test = preg_replace("[^a-zA-Z0-9-]", "", $test);
    echo $test."\n";
?>

I want to delete all chars which aren't letters, numbers or a minus

What's my mistake?

1 Answer 1

4

delimiter is missing

$test = preg_replace('/[^a-zA-Z0-9-]/', '', $test); 
echo $test . "\n";

Additionally, I recommend using PHP_EOL instead of "\n" for newline characters.

Sign up to request clarification or add additional context in comments.

1 Comment

note: if you use: $test = preg_replace('/[^a-zA-Z0-9-]+/', '', $test); it will be faster because it need to do less operations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.