Jump to content

How do I replace any number of character occurences with one occurrence?


OM2

Recommended Posts

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

Link to comment
Share on other sites

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  :confused:

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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    :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.