变量和preg_match PHP

时间:2014-12-22 07:33:11

标签: php regex

我正在创建一个应用程序,该应用程序仅显示已将某个配置文件名附加到文本开头的图片。

例如文件夹中的3个文件是:Rogue-fastcar.jpg,John-girls.png,Petra-moneyNmore.jpg

我想只返回包含会话中存储的配置文件的图片。到目前为止,它显示了所有用户的图片。

感谢您的帮助,非常感谢。

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               
            );
        }
    }
}

5 个答案:

答案 0 :(得分:2)

似乎你有一些问题逃避引号和连接字符串。

看起来你似乎还有一种迂回的方式去做你想做的事。

我们能做的是:

// 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
            );
        }
    }
}

然而,要解决为什么它首先没有工作的原因:

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               
            );
        }
    }
}

答案 1 :(得分:1)

$ pattern =' /.$ profile./' ;;中的点应该匹配任何不是换行符的字符。你可能想尝试这样的事情:

$ pattern =' / ^ $个人资料[ - ] {1}。* /'这应该说你希望匹配以配置文件名称开头的任何内容,一个破折号,然后是其他任何东西。 (。除了/ n字符以外的任何内容,*是前一个模式的0或更多)希望这会有所帮助。

答案 2 :(得分:0)

嗯,PHP代码中至少存在两个问题:首先是语法错误:

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

试试这个:

$profile = $_SESSION["profile"];   

然后你的正则表达式是错误的,因为.匹配任何字符,但它只匹配一个。因此,您需要使用*代替匹配任何字符和任何多重性。

答案 3 :(得分:0)

指出有更聪明的方式(更具可读性)来获取文件的扩展名

$extension = pathinfo($image, PATHINFO_EXTENSION);

答案 4 :(得分:0)

使用此代码从配置文件名称为

的文件夹中获取图像
$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);
  }
}