1

When an key is pressed in an input I call this on its value

.trim().replace(/ /g, '.').replace(/[^\w .-]/gi, '').toLowerCase();

what I want to do is, when the value is submitted to the server I want to do a function that will take the value, fun the php version of this function over it, and if the new string is different to the original string return false, else return true.

So, What is the php equivelent of the above line of code?

obviously I can use strtolower(str_replace(' ', '.', trim($value)))

which means I just need to equivelent of .replace(/[^\w .-]/gi, '')

3 Answers 3

1
preg_replace('/[^\w .-]/', '', $subject);

Notes: The global flag g is not needed in php. Flag i is not necessary for \w.

And as a side note: You don't actually need the space in the character class because you removed spaces already.

Sign up to request clarification or add additional context in comments.

Comments

1

Could it be preg_replace(pattern, replacement)?

http://php.net/manual/en/function.preg-replace.php

3 Comments

@Hailwood: If you add delimiters, yes. Googling for php regular expression revealed: regular-expressions.info/php.html I suggest to have a look at it.
It should. Here's the syntax rerefence for PCRE (used in preg_ functions): regextester.com/pregsyntax.html
Like Felix said, you need to note it as a string literal (JavaScript has Regex literals; PHP does not), so just wrap it around with ' quotes.
1

looks like your found strtolower and trim on your own. to replace something using a regular expression, simply use preg_replace (or preg_filter if the replacement should be a regular expression, too).

depending on wich version of php you're using, theres also ereg_replace, but you shouldn't rely on that as it's deprecated. if possible, use preg_replace/preg_filter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.