Jump to content

Recommended Posts

Hello,

 

I'm looking to format a string and make sure that it only contains the letters A-Z and numbers 0-9. I also would like to make sure that there's only ever a singe space between words.. no extra line-feeds or returns or double spaces.

 

How would I first verify that a string only contains letters or numbers? After that how would I change the spacing so there's only ever a single space between words or numbers?

Link to comment
https://forums.phpfreaks.com/topic/242467-formatting-a-string/
Share on other sites

How would I first verify that a string only contains letters or numbers?

Regular expressions, specifically preg_match.

 

After that how would I change the spacing so there's only ever a single space between words or numbers?

Probably use str_replace, or if you need to replace something a little more complicated, use preg_replace.

Link to comment
https://forums.phpfreaks.com/topic/242467-formatting-a-string/#findComment-1245315
Share on other sites

There is formatting, and there is validation -- 2 entirely different things.  If what you mean is that you want to accept input, and then automatically conform that input to these rules, then you could do that with preg_replace.  You replace characters that aren't in your acceptable list with nothing.  The acceptable list is very easy using a regex character class

 

"/[^A-Z0-9 ]/"

 

That will find any character that is not uppercase A-Z or 0-9 or space.  It specifically only allows spaces.

 

Then you would want to make a 2nd pass and find multiple spaces and replace those with a single space, however I'd throw in a trim first to remove any leading spaces. The other thing you could do would be to explode on spaces and rebuild the string from the exploded array, with only one space between.

 

For the preg_replace, this would find occurrences where you have more than one space.

 

"/[ ]{2,}/"

 

 

Link to comment
https://forums.phpfreaks.com/topic/242467-formatting-a-string/#findComment-1245316
Share on other sites

Do you want to verify that the string only contains valid characters and reject it if it is wrong? OR Do you want to change any invalid characters to something else? You say you want to verify the string only contains letters and numbers, and then make sure that there are only single spaces ... - If you validate that there are only letters and numbers, then there are not any spaces.

 

It sounds like you have user input, and you want to "clean it up". If this is the case, I would suggest the following:

[*]Replace all occurrences of CR-NL, CR, and NL with a single space (use str_replace);

[*]Replace all occurrences of two-or-more spaces with a single space (use preg_replace with a pattern like: ~ {2,}~);

[*]Check for instances of not-valid characters (use preg_match with a pattern like: ~[^A-Z0-9 ]~i)

 

You could actually accomplish #1 and #2 with a single call to preg_replace(). In either case, the array of patterns (or strings) to match need to be listed in the order given above.

 

If the preg_match() (#3) returns 1 (one), then there are invalid characters. If it returns 0 (zero) then there are NO invalid characters. If your desire is to change the invalid characters to something specific, you can use preg_replace() in #3, instead.

 

The following code is not tested:

$input = 'User entered 1,203 characters of bald a$$ lies';

$find = array("~\r\n~", "~\r~", "~\n~", "~ {2,}~");
$input = preg_replace($find, ' ', $input);

if (preg_match("~[^A-Z0-9 ]~i", $input) != 0) trigger_error("Invalid Input", E_USER_ERROR);

//OK

Link to comment
https://forums.phpfreaks.com/topic/242467-formatting-a-string/#findComment-1245318
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.