Page 1 of 1

Looking at characters in a string

Posted: Fri Nov 18, 2005 8:07 am
by jayshields
Ok, it was hard to sum up this problem in the subject, anyway, how can I look at a string and see if there is only one character used in it, and if it's the character i specified.

for example:
I've got a variable called $dir. If it contains "/", "//", "///", and so on, i want to return false. If it contains "////////d", "/df/df/s/dsf/sd/f/", "dfsfs/f/////" then it should return true. So, I want a function that will look at a string, and if it contains one character, whether the string is one character long or 50 chars long, it will return true or false.

i couldn't find anything on php.net or using search on here so i hope you can understand me!

cheers guys.

Posted: Fri Nov 18, 2005 8:19 am
by BDKR
It looks to me like you are looking for strings. Not individual characters.

What's wrong with strstr() or stristr()?

Maybe I'm not understanding your post. Forgive me if that's the case.

Posted: Fri Nov 18, 2005 8:34 am
by jayshields
All strstr does is look for a character in a string, and if it's found returns everything after it, stristr is just the case insensitive version.

Anyway, that wouldn't work in my situation. If $dir was set to "////" and I did $newdir = strstr($dir, "/"); then $newdir would be "////", which is the same.

I think I need to combine 2 functions to get my desired result, but I can't figure out how. I also reckon it's doable with regex, but I'm hopeless with regex and probably wouldn't understand it anyway, I'd prefer something I understood :)

I'll try explain again:
If a string contains the usage of just one character ("/"), be it a string with the same character 23 times ("///////////////////////") or the same character just once ("/"), I want it to return true. If that string contains the usage of more than one character (be it including the chosen char or not...) ("//////f", "dsfnn"), then it would return false.

So in my situation, I would set the character as "/".

Hopefully someone understands my problem!! :)

Posted: Fri Nov 18, 2005 8:35 am
by josh
If I understood you correctly, you did a good job explaining yourself the first time, I think the first replier was just confused. If your one character is constant use this regex

Code: Select all

#/+#
if not, we'll have to get a regex guru in here :P

Posted: Fri Nov 18, 2005 8:43 am
by Jenk
so if the string contains nothing but a forward slash, it is invalid, but if there is any other character in that string, it is valid? if so:

Code: Select all

preg_match('#^/+$#', $string);

Posted: Fri Nov 18, 2005 8:43 am
by jayshields
jshpro2 wrote:If I understood you correctly, you did a good job explaining yourself the first time, I think the first replier was just confused. If your one character is constant use this regex

Code: Select all

#/+#
if not, we'll have to get a regex guru in here :P
As I said, I'm hopeless with regex!

How would I implement that?

Code: Select all

$test = reg("#/+#", $string);
That's just a wild guess.

edit: ah, i missed that post Jenk. So what would preg_match return? False if it found only the slashes were used? Or something else? :?

Posted: Fri Nov 18, 2005 8:51 am
by Jenk
See my previous reply :)

Posted: Fri Nov 18, 2005 9:00 am
by josh
I forgot the ^ and $, but jenk's code should do exactly what you're asking

Posted: Fri Nov 18, 2005 9:25 am
by jayshields
Yeah, it worked a treat. Cheers guys.