次の操作の実行に問題があります..
http://www.google.com --> www.google.com/
https://google.com --> www.google.com/
google.com --> www.google.com/
を削除しようとしており、URL の先頭に が追加されているhttps:// or http://
ことを確認してwww.
から、URL に末尾のスラッシュが存在しない場合は追加します。
この大部分は理解できたように感じますが、思い通りstr_replace()
に作業を進めることができません。
私の理解では、これは使用方法str_replace
です:
$string = 'Hello friends';
str_replace('friends', 'enemies', $string);
echo $string;
// outputs 'Hello enemies' on the page
これが私がこれまでに持っているものです:
$url = 'http://www.google.com';
echo reformat_url($url);
function reformat_url($url) {
if ( substr( $url, 0, 7 ) == 'http://' || substr( $url, 0, 8 ) == 'https://' ) { // if http:// or https:// is at the beginning of the url
$remove = array('http://', 'https://');
foreach ( $remove as $r ) {
if ( strpos( $url, $r ) == 0 ) {
str_replace($r, '', $url); // remove the http:// or https:// -- can't get this to work
}
}
}
if ( substr( $url, 0, 4 ) != 'www.') { // if www. is not at the beginning of the url
$url = 'www.' . $url; // prepend www. to the beginning
}
if ( substr( $url, -1 ) !== '/' ) { // if trailing slash does not exist
$url = $url . '/'; // add trailing slash
}
return $url; // return the formatted url
}
URL をフォーマットする方法についてご支援いただければ幸いです。また、http://またはhttps://を削除するためにstr_replaceで間違っていることについてもっと興味があります。私が間違っていることについて誰かが洞察を提供できれば、それは大歓迎です。