Looking at characters in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Looking at characters in a string

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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!! :)
Last edited by jayshields on Fri Nov 18, 2005 8:36 am, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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);
Last edited by Jenk on Fri Nov 18, 2005 8:43 am, edited 1 time in total.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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? :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

See my previous reply :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I forgot the ^ and $, but jenk's code should do exactly what you're asking
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Yeah, it worked a treat. Cheers guys.
Post Reply