こんにちは、初めて preg_replace を扱い、特に学習者を理解するのが非常に複雑であることがわかりました。
タイトル文字列を URL 構造のスラッグに変更して、すべての特殊文字を削除し( ) ? *
、複数のスペースを単一に置き換えて-
、すべてのテキストを小文字に変換しようとしています。
ここに私の面白いコードがありますが、欲望の出力が得られません。
$title_slug = $q_slug->title;
$title_slug = preg_replace("/[\s+\?]/", " ", $title_slug);
$title_slug = str_replace(" ", " ", $title_slug);
$title_slug = str_replace(" ", "-", $title_slug);
$title_slug = preg_replace("/[^\w^\_]/"," ",$title_slug);
$title_slug = preg_replace("/\s+/", "-", $title_slug);
$title_slug = strtolower($title_slug);
return $title_slug;
編集:例を追加
例:if my title is what is * the() wonder_ful not good?? and where???
結果:if-my-title-is-what-is-the-wonder_ful-not-good-and-where
お気軽に笑ってください:)そして、助けてくれてありがとう。