开发者

convert javascript (regex) function to php

开发者 https://www.devze.com 2023-03-15 09:04 出处:网络
When an key is pressed in an input I call this on its value .trim().replace(/ /g, \'.\').replace(/[^\\w .-]/gi, \'\').toLowerCase();

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 i开发者_如何学Gos 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, '')


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.


Could it be preg_replace(pattern, replacement)?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消