Haris
Haris

Reputation: 1059

Detect random strings

I am building a string to detect whether filename makes sense or if they are completely random with PHP. I'm using regular expressions.

A valid filename = sample-image-25.jpg

A random filename = 46347sdga467234626.jpg

I want to check if the filename makes sense or not, if not, I want to alert the user to fix the filename before continuing.

Any help?

Upvotes: 1

Views: 1852

Answers (2)

Shoe
Shoe

Reputation: 76240

You need way to much work on that. You should make an huge array of most-used-word (like a dictionary) and check if most of the work inside the file (maybe separated by - or _) are there and it will have huge bugs.

Basically you will need of

  • explode()
  • implode()
  • array_search() or in_array()

Take the string and look for a piece glue like "_" or "-" with preg_match(); if there are some, explode the string into an array and compare that array with the dictionary array.

Or, since almost every words has alternate vowel and consonants you could make an huge script that checks whatever most of the words inside the file name are considered "not-random" generated. But the problem will be the same: why do you need of that? Check for a more flexible solution.

Notice: Consider that even a simple-and-friendly-file.png could be the result of a string generator.

Good luck with that.

Upvotes: 2

ssokolow
ssokolow

Reputation: 15345

I'm not really sure that's possible because I'm not sure it's possible to define "random" in a way the computer will understand sufficiently well.

"umiarkowany" looks random, but it's a perfectly valid word I pulled off the Polish Wikipedia page for South Korea.

My advice is to think more deeply about why this design detail is important, and look for a more feasible solution to the underlying problem.

Upvotes: 5

Related Questions