最近开始学php
为 实现一段字符串 去掉多余空格, 去掉非[a-zA-Z0-9], 全部变小写, 空格变成-. 这样就能当url使用了
大家看看合理不? 需要怎么改进
<?php
$string=" This is a strin g-test width åäö and some über+stra nge characters: _like this?";
echo str_replace(" ","-",strtolower(trim(preg_replace("/[\n| ]{2,}/"," ",preg_replace("/[^[:alnum:] ]/", '', $string))))).'<br>';
结果是 this-is-a-strin-gtest-width-and-some-berstra-nge-characters-like-this
为 实现一段字符串 去掉多余空格, 去掉非[a-zA-Z0-9], 全部变小写, 空格变成-. 这样就能当url使用了
大家看看合理不? 需要怎么改进
<?php
$string=" This is a strin g-test width åäö and some über+stra nge characters: _like this?";
echo str_replace(" ","-",strtolower(trim(preg_replace("/[\n| ]{2,}/"," ",preg_replace("/[^[:alnum:] ]/", '', $string))))).'<br>';
结果是 this-is-a-strin-gtest-width-and-some-berstra-nge-characters-like-this