php define path using preg_match/preg_replace?

Asked

Viewed 37 times

1

I have a string = "/var/www*/te'st/test.php" and I want to be able to clear all the special characters to get a string like /var/www/test/test.php

$string = "/var/www*/te'st/test.php";
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);
  • The problem you have with this regex at the moment

  • @Isac from what I understand, he wanted the string /var/www/test/test.php. This regex he used, returns varwwwtesttestphp

  • 1

    @Andreicoelho Yes I also tested and realized this, but who looks at the question may not be able to understand what the problem the author is having. If you stay in the question it’s clearer to everyone.

  • @Isac yes.. has rasão!

1 answer

1


Do it like this:

$string = "/var/www*/te'st/test.php";
$string = preg_replace( '/[^A-Za-z0-9\-\\.\/]/' , '' , $string );
echo $string;

See it working on Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.