Search code examples
phppreg-match

preg_match if first char is slash


I try to find out if the first char of a string is a slash.

I have this string /var/www/html

$mystring = "/var/www/html";
$test = preg_match("/^/[.]/", $mystring);

if ($test == 1)
{
    echo "ret = 1";
}
else 
{
    echo "ret = 0";
}

But I always get ret = 0.


Solution

  • Try this:

    <?php
    $mystring = "/var/www/html";
    $test = preg_match("/^\//", $mystring);
    
    if ($test == 1)
    {
        echo "ret = 1";
    }
    else 
    {
        echo "ret = 0";
    }