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
https://forums.phpfreaks.com/topic/183175-compare-strings-for-full-words/
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);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.