0

I am currently using

preg_replace('/[^a-zA-Z0-9]+/', '_', $str)

what would be the alternative function using preg_replace_callback?

3
  • 1
    Why do you want to do that? This would only call an unnecessary function call which you don't need here Commented Jul 16, 2015 at 9:02
  • What have you tried? You already have most parts (esp. the regex), a function that returns '_' shouldn't be too difficult to implement - or is it? Commented Jul 16, 2015 at 9:03
  • Yes, can you please do it for me? Commented Jul 16, 2015 at 10:02

1 Answer 1

1

I did it myself

function url_title($mystr){
	$result = '';
	$result .= preg_replace_callback(
                '/[^a-zA-Z0-9]+/',
                function ($matches) { return '_'; }, 
                $mystr);
						
	return strtolower($result);
}

$mystr = "some string here";
echo url_title($mystr);

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

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.