Haroldo
Haroldo

Reputation: 37377

quick [php function] -> [javascript function] question

if anyone fancies doing me a really quick favour, it would be really appreciated:

static function make_url_safe($z){
    $z = strtolower($z);
    $z = preg_replace('/[^a-zA-Z0-9\s] /i', '', $z);
    $z = str_ireplace(' ', '-', $z);
    return $z;
}

what js functions should i be looking at to convert this function to javascript?

Upvotes: 1

Views: 88

Answers (4)

James
James

Reputation: 111960

function makeURLSafe(z){
    return z.toLowerCase().replace(/[^a-z0-9\s]+/g, '').replace(/ /g, '-');
}

Upvotes: 1

SilentGhost
SilentGhost

Reputation: 319881

var s = 'Abc- sdf%$987234'.toLowerCase();
s.replace(/[^a-z0-9\s]+/g, '').replace(/ /g, '-');

It's not an exact equivalent, because your original function makes little sense: using i flag after converting string to lower case, using a-zA-Z with i flag on lower-case string, random space after the character class, str_ireplace with space as a first parameter.

Upvotes: 6

Take a look at String and RegExp classes.

Upvotes: 1

deceze
deceze

Reputation: 522500

Not equivalent, but has the same goal of making strings URL safe:
encodeURI or encodeURIComponent

Upvotes: 1

Related Questions