-2

This is the problem that I have in PHP and jQuery. I want to make a "smart" function that will not only clean white space, but also a certain character at the beginning and end of the string. Why?

I made administration panel who manage with subdomains. Allowed characters is [a-z0-9-.]. If someone accidentally write "-my.domanin." or ".my-domain" or "-my-domain-" or a similar variation is problem. I need to trim that to look like this "my-domain" or "my.domain"

Is this possible? Thanks!

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
  • you can add character mask to `trim()`. See the docs – rlatief Aug 04 '14 at 19:06
  • 1
    Just so you need the dot `.` is not a part of the set `[a-z0-9-]` so `my.domain` is an invalid name. Also, if you want to use regex and substitution [check this out](http://regex101.com/r/tM9pF8/1) – skamazin Aug 04 '14 at 19:09
  • OK, I forgot to look trim documentation on PHP but jquery don't have mask. Thanks @skamazin this is helpful. – Ivijan Stefan Stipić Aug 05 '14 at 11:08

1 Answers1

2
function mytrim($stringToTrim) {
    return trim($tringToTrim, " \t\n\r\0\x0B.-");
}

The first part of the string, " \t\n\r\0\x0B", strips white space from the beginning and ends and the second part, ".-" strips the "." and "-" characters.

Clearly you could just use trim() in your script, I just put it in a function for the example.

Kvothe
  • 1,819
  • 2
  • 23
  • 37