0

I have a folder structure and two folders are named up as ComputerScience and Science

The trouble is my code always detects the variable as containing science when i need to match them both.

Im using

if (strpos($dir, "ComputerScience") !== FALSE) {
    $my_full_path= $dir;
    $sql = "UPDATE documents 
                SET subject = 'ComputerScience' 
            WHERE documentname='".$dir."'";
    mysqli_query($link,$sql);
}

and

if (strpos($dir, "Science") !== FALSE) {
    $my_full_path= $dir;
    $sql = "UPDATE documents 
                SET subject = 'Science' 
            WHERE documentname='".$dir."'";
    mysqli_query($link,$sql);
}

Im guessing the second block of code is overwriting the first with being matched, how can i do an exact match of the string if $dir variable contains it?

Thanks

2

3 Answers 3

2

Ideally you would add directory separators to avoid these problems:

if (strpos($dir, "/ComputerScience/") !== FALSE) {
}
if (strpos($dir, "/Science/") !== FALSE) {
}

Replace / with the actual directory separator for your file system (\ or /).

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

3 Comments

@Salaman A there's a constant DIRECTORY_SEPARATOR that defines OS directory separator
@robert but it might be different from what is stored in the db. E.g. on windows it is \ but the column might be storing URLs that use /.
that's why dev env should be the same as in prod(docker it is)
1
$subject = FALSE !== strpos($dir, "ComputerScience") ? 'ComputerScience' : (FALSE !== strpos($dir, "Science") ? 'Science' : null);

if (null !== $subject) {
    $my_full_path= $dir;
    $sql = "UPDATE documents 
                SET subject = '$subject' 
            WHERE documentname='".$dir."'";
    mysqli_query($link,$sql);
}

also it would be good to use prepared statement. They point is that the code above reduces boilerplate because you don't need to write similar code with execution of queries twice. The same can be done with if solution as @Salman A did. It's just another option with ternary operators.

Comments

0

If you're matching sub-strings, you should always look for the longer match in preference to the shorter one:

if (strpos($dir, "ComputerScience") !== FALSE) {
    $sql = "UPDATE documents 
                SET subject = 'ComputerScience' 
            WHERE documentname='".$dir."'";
    mysqli_query($link,$sql);
} elseif (strpos($dir, "Science") !== FALSE) {
    $sql = "UPDATE documents 
                SET subject = 'Science' 
            WHERE documentname='".$dir."'";
    mysqli_query($link,$sql);
}

You can shorten your code by storing the matches in an array first:

$matches = ['ComputerScience', 'Science'];
foreach ($matches as $match) {
    if (strpos($dir, $match) !== FALSE) {
        $sql = "UPDATE documents 
                    SET subject = '$match' 
                WHERE documentname='$dir' ";
        mysqli_query($link, $sql);
        break;
    }
}

Again, the longer match goes first in the array. You can also use string interpolation to make the SQL easier to read.

Comments

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.