Looking at characters in a string
Moderator: General Moderators
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Looking at characters in a string
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.
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.
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
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!!
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.
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
if not, we'll have to get a regex guru in here 
Code: Select all
#/+#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.
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
As I said, I'm hopeless with regex!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
if not, we'll have to get a regex guru in hereCode: Select all
#/+#
How would I implement that?
Code: Select all
$test = reg("#/+#", $string);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?
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England