Damian Anthony
Damian Anthony

Reputation: 25

Variables and preg_match PHP

I am creating an app that displays only pictures where a certain profilename as been appended on to the beginning of the text.

for example 3 files in the folder are : Rogue-fastcar.jpg , John-girls.png, Petra-moneyNmore.jpg

I would like to return only the picture that has the profile that is stored within the session. So far its showing pictures from all users.

Thanks for the assistance, much appreciated.

foreach ($images as $index => $image) {
    $extension = strtolower(end(explode('.', $image)));
    $subject = $image;
    $profile = "$_SESSION["profile"]";
    $pattern = '/.$profile./';

    preg_match($pattern, $subject, $matches);



    if ($matches) {
        echo "Worse Behavior";
        if(!in_array($extension, $extensions)){
            unset($images[$index]);
        }else{
            $images[$index] = array(
                'full' => $this->path. '/' . $image,
                'thum' => $this->path. '/thumb/' . $image               
            );
        }
    }
}

Upvotes: 2

Views: 497

Answers (5)

Hussain Hamdani
Hussain Hamdani

Reputation: 64

Use this code to get image from folder with profile name

$pathToImagesFolder = "/images/"; // path to your images folder
//
$profileName = $_SESSION["profile"];
// Open a directory, and read its contents
if (is_dir($pathToImagesFolder)){
  if ($dh = opendir($pathToImagesFolder)){
    while (($file = readdir($dh)) !== false){
        if($file != '..' && $file != '.') {
                $fileDetails = explode('.',$file);
                if(is_array($fileDetails) && $fileDetails[0] == $profileName) {
                        echo $file; //Image with extension
                        //Your Code
                }
        }
    }
    closedir($dh);
  }
}

Upvotes: 0

flod
flod

Reputation: 113

Just pointing out that there are smarter ways (and more readable) to get a file's extension

$extension = pathinfo($image, PATHINFO_EXTENSION);

Upvotes: 0

Kavi Siegel
Kavi Siegel

Reputation: 2994

It seems you were having a few issues escaping quotes and concatenating strings.

It also looks like you're going a bit of a roundabout way to do what you'd like.

What we could do is:

// Get length of $_SESSION['profile']
$profileLength = strlen($_SESSION['profile']);

// Get that amount of characters from the image name starting at the first character
$imagePrefix = substr($image, 0, $profileLength);

// Compare the first X characters of image names to $_SESSION['profile']
if($imagePrefix == $_SESSION['profile']){..}

// Put it together now:
foreach ($images as $index => $image) {
    $extension = strtolower(end(explode('.', $image)));
    // $subject = $image;
    // $profile = "$_SESSION["profile"]"; 
    // $pattern = '/.$profile./';
    // preg_match($pattern, $subject, $matches);

    // if ($matches) {

    $profileLength = strlen($_SESSION['profile']);
    $imagePrefix = substr($image, 0, $profileLength);

    if($imagePrefix == $_SESSION['profile']){
        echo "Worse Behavior";

        if(!in_array($extension, $extensions)){
            unset($images[$index]);
        }else{
            $images[$index] = array(
                'full' => $this->path. '/' . $image,
                'thum' => $this->path. '/thumb/' . $image
            );
        }
    }
}

However, to address the reasons why it didn't work in the first place:

foreach ($images as $index => $image) {
    $extension = strtolower(end(explode('.', $image)));
    $subject = $image;

    // This should not be quoted
    // $profile = "$_SESSION["profile"]"; 
    $profile = $_SESSION["profile"]; 

    // The concatenation here is close, but you forgot some single quotes
    // $pattern = '/.$profile./';
    $pattern = '/'.$profile.'/';

    // Not only that, but you can combine the two lines into:
    $pattern = '/' . $_SESSION['profile'] . '/';

    // Furthermore, when putting anything into a regular expression, you should sanitize:
    $pattern = '/' . preg_quote($_SESSION['profile']) . '/';

    preg_match($pattern, $subject, $matches);

    // $matches may be interpreted as true or false depending on array length. Try instead..
    // if ($matches) {
    if (count($matches) > 0) {
        echo "Worse Behavior";
        if(!in_array($extension, $extensions)){
            unset($images[$index]);
        }else{
            $images[$index] = array(
                'full' => $this->path. '/' . $image,
                'thum' => $this->path. '/thumb/' . $image               
            );
        }
    }
}

Upvotes: 2

bio_sprite
bio_sprite

Reputation: 448

the dots in your $pattern = '/.$profile./'; should be matching to any character that is not a newline. You probably want to try something like this:

$pattern= '/^$profile[-]{1}.*/' This should say you want to match anything that starts with the profile name, one dash, and then followed by anything else. (. is anything except the /n character and * is 0 or more of the previous pattern) Hope this helps.

Upvotes: 1

mgaido
mgaido

Reputation: 3055

Well, there are at least two problems in your PHP code: first of all this is a syntax error:

$profile = "$_SESSION["profile"]";

try with this:

$profile = $_SESSION["profile"];   

then your regex is wrong, because . matches any character, but it matches just one. Thus you need to use * instead which matches any character and any multiplicity too.

Upvotes: 0

Related Questions