문제

How would I replace anything but letters and numbers with a hyphen? This would be for a slug URL where a person would type in the string they would like for their URL. The problem I’m having is people inputting symbols and many spaces which make the URL invalid.

Also, what would be a good resource to test this so that I don’t have to ask again? Thanks.

도움이 되었습니까?

해결책

The code

$input_url = "This is only an example: do you like it?";
$slug_url = preg_replace("/[^a-zA-Z0-9]/", "-", $input_url);
print($slug_url);

will output

This-is-only-an-example--do-you-like-it-

DEMO

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top