PHP ile altdizgelerin nasıl ayıklanır

4 Cevap php

PHP acemi bir soru.

Ben yönetici arkaplanı için veritabanında aşağıdaki gibi görüntü yolları tutmak gerekir.

../../../../assets/images/subfolder/myimage.jpg

Ön uç için aşağıdaki gibi Ancak ben görüntü yolları gerekir.

assets/images/subfolder/myimage.jpg

PHP ile bu durumu değiştirmek için en iyi yolu nedir?

I substr () hakkında düşündüm, ama daha iyi yollar var olup olmadığını merak ediyorum.

Şimdiden teşekkürler.

4 Cevap

Bir application variable daki görüntü yolunu kaydetmeli ve admin ve önyüz hem de erişebilirsiniz

.. / .. / .. / .. / Sabit ise, o zaman substr çalışacaktır. Eğer değilse, böyle bir şey deneyin:

newpath=substr(strpos(path, "assets"));

It might seem like an odd choice at first but you could use ltrim. In the following example, all ../'s will be removed from the beginning of $path. The dots in the second argument have to be escaped because PHP would treat them as a range otherwise.

$path = ltrim('../../../../assets/images/subfolder/myimage.jpg', '\\.\\./');

$path o olacak:

assets/images/subfolder/myimage.jpg

Ben bu önermek

$path = "../../../../assets/images/subfolder/myimage.jpg";
$root = "../../../../";

$root_len = strlen($root);

if(substr($path, 0, $root_len) == $root){
    echo substr($path, $root_len);
} else {
    //not comparable
}

Bu şekilde size görüntüler için root olarak dikkate hangi dizinde kontrolü bir tür var