Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match if first char is slash

Tags:

php

preg-match

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.

like image 570
Black Avatar asked Feb 07 '23 10:02

Black


2 Answers

Try this:

<?php
$mystring = "/var/www/html";
$test = preg_match("/^\//", $mystring);

if ($test == 1)
{
    echo "ret = 1";
}
else 
{
    echo "ret = 0";
}
like image 124
Ren Camp Avatar answered Feb 10 '23 02:02

Ren Camp


You can simply use strpos() for that:

<?php
    $mystring = "/var/www/html";
    if(strpos($mystring,"/") === 0){
        echo "ret = 1";
    }else{
        echo "ret = 0";
    }
?>    
like image 36
mitkosoft Avatar answered Feb 10 '23 00:02

mitkosoft