0

I'm looking for a way to test on a portion of a RecursiveDirectoryIterator's object output. I know how to identify a folder, but I am new to php and unfamiliar with how to look at the contents of the folder object's path... Is this possible?

I have the following:

foreach($files as $object){
    $indent = str_repeat('   ', $files->getDepth());
    if($object->isDir()){
        //I know this is wrong... here is where I would want to find the string:
        if($object contains 'mystring')){
            echo "%%%-------found the string!\n";
        }else{
            echo $indent, "|| dir: $object\n";
        }   
    }else{
        echo $indent, "-- $object\n";
    }   
}   

1 Answer 1

1

replace the line

if($object contains 'mystring')){

with:

if(strpos($object->getPathname(), 'mystring') === false){

that will search the object's full path for the string 'mystring' and return boolean false if 'mystring' is not part of the path

btw, string concatenation in php is with the dot, not the comma, so you might want to do echo $indent."-- $object\n"; afterwards

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

2 Comments

I think you meant '==' instead of '===', correct?? SO won't let me edit this change because there are too few characters.
no, it's ===. because strpos returns boolean false if the search term is not contained, and 0 if the string starts with the search term. So if you use ==, it may evaluate to "false" even if 'mystring' is part of the string. See the warning in php.net/strpos

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.