1

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?

4 Answers 4

6
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.

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

Comments

1

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

2 Comments

i need to make an exact equivalent. So rather than using a built in url encoding function i need to use some smaller native js functions like .replace(), combined to make my make_urol_safe() function
In that case, go with @SilentGhosts answer.
1

Take a look at String and RegExp classes.

Comments

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

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.