Jump to content

compare strings for full words?


jacko310592

Recommended Posts

hey everyone

 

<?php
$galleryDir = '';
foreach(glob("*", GLOB_ONLYDIR) as $dir)
     $galleryDir .= "$dir\n";
   
$strArr = explode("?", $_SERVER["REQUEST_URI"]);
$thisDirectory = $strArr[sizeof($strArr) - 1];

$pageCorrect = strpos($galleryDir, $thisDirectory);

if ($pageCorrect === false) {
   header('Location: ./index.php');
   exit (0);
}
?>

 

as you see with the above code, on this line: "$pageCorrect = strpos($galleryDir, $thisDirectory);" it is looking for any charachters within $thisDirectory which are the same as those within the $galleryDir string,

but is there a way to make it so not only do the charachers have to be the same, but it has to be the full word?

 

within the string, there are several values, each of the single worded values need to be separate though, so im looking to change to code so it will look for full words which are the same, not single characters, nor the entire string.

 

can anyone suggest a way this can be done

 

thanks guys

Link to comment
Share on other sites

This can be done using PCRE functions, in particular preg_match, and the \b modifier, however in your case I'd opt for another option. It would be better to just store glob("*", GLOB_ONLYDIR) as a variable, to get a string ($galleryDir) from that you can get $galleryDir by implode(...). You can then use in_array to check if the directory exists. Another option would be to use file_exists, this would be better if you don't actually need the list of directories for any other processing.

 

<?php
$dirs = glob("*", GLOB_ONLYDIR);
$galleryDir = implode("\n", $dirs);
   
$strArr = explode("?", $_SERVER["REQUEST_URI"]);
$thisDirectory = $strArr[sizeof($strArr) - 1];

if (!in_array($thisDirectory, $dirs)) {
   header('Location: ./index.php');
   exit (0);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.