OM2 Posted October 13, 2009 Share Posted October 13, 2009 How do I replace any number of character occurences with one occurrence? Let's say I have: aaa aaaaaa aaaaa aaaaaaaa All of the above should be replaces with a single 'z' character say for example I can do exact matches, say for 'aaaaaa' - but am unsure how I can do for any number I'm sure it should be possible to do using one line of code - but I'm just not sure where to start! Thanks OM Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 13, 2009 Share Posted October 13, 2009 You might want to have a look at str_replace: http://www.php.net/manual/en/function.str-replace.php If you check out the optional $count variable you should be able to figure out a way of creating a loop to do this for you. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted October 13, 2009 Share Posted October 13, 2009 Or use preg_replace Quote Link to comment Share on other sites More sharing options...
OM2 Posted October 13, 2009 Author Share Posted October 13, 2009 oh i thought there would be a more elegant regular expression solution i can easily write a function that does unlimited number of loops where one character at a time is removed until we get left with a single character but it would have thought this would be highly inefficient way and was (blindly) sure that there would be a better way! hmmm Quote Link to comment Share on other sites More sharing options...
OM2 Posted October 13, 2009 Author Share Posted October 13, 2009 Or use preg_replace aaah... thats more like it the problem is that i'm unsure about the weird regular expressions! i read the function up and i think there's something that does what i need: <?php $str = 'foo o'; $str = preg_replace('/\s\s+/', ' ', $str); // This will be 'foo o' now echo $str; ?> now... i'm not sure how the regular expression works! how do u make sure that u r just left with one single space? how does it only search for 2 or more spaces - but ignores single spaces? what i actually want to do is replace all space characters with a '-' character if there is a single space, then this gets replaced with a single '-' if there is 2 or more spaces, then these also get replaced by a single '-' character not sure where to start! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 $str = preg_replace('%\s+%','-', $str); Quote Link to comment Share on other sites More sharing options...
OM2 Posted October 14, 2009 Author Share Posted October 14, 2009 thank u! trying out now Quote Link to comment Share on other sites More sharing options...
cags Posted October 14, 2009 Share Posted October 14, 2009 The \s will match any whitespace character is that the intention or do you just with to replace the space character? If the latter then you can use this regex pattern. ~[ ]+~ Just incase somebody decides to point it out, this will work just as well... ~ +~ ... it's up to you as to which you find easier to read. Personally I find the first easier at a glance. Quote Link to comment Share on other sites More sharing options...
OM2 Posted October 14, 2009 Author Share Posted October 14, 2009 thank u - that helps a lot it's probably just space character that i want to catch - but i think i'd go for whitespace - just incase what i've got is a system where users can duplicate directories i want the user to be only allowed to enter characters and numbers when choosing a new name for the duplicated directory ALL other characters should be left out AND white space characters need to be replaced with a single '-' (no matter how many of them) hmmm... the more i think of it... i think what i want could be done in a regular expression? not sure + i'm a little confused when it comes to reading or writing them Quote Link to comment Share on other sites More sharing options...
cags Posted October 14, 2009 Share Posted October 14, 2009 You could have probably used something like... preg_match("~^[a-z_-]{3,8}$~i", $input); ... to validate the string. That will check that $input is a string that is between 3 and 8 characters long, contains only the values a trough z underscore and dash (or whatever you wish to call it). The i at the end makes it case insensitive so you can use upper or lower case. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 if you want to do that it's pretty easy $input = 'random string for 4352345 your DiReCtOrY'; $input = preg_replace('/[^\w\s]+/i','', trim($input)); $input = preg_replace('/\s+/','-', trim($input)); Quote Link to comment Share on other sites More sharing options...
OM2 Posted October 14, 2009 Author Share Posted October 14, 2009 awesome: thank u for all the replies! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 Just realised I copied and pasted the trim for the second preg_replace. That shouldn't be there Quote Link to comment Share on other sites More sharing options...
OM2 Posted October 14, 2009 Author Share Posted October 14, 2009 i noticed before as well thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.