How can i remove $_SERVER['DOCUMENT_ROOT'] from a string like this
/home/bla/test/pic/photo.jpg
the result should look like this
/test/pic/photo.jpg
I also need to take the photo.jpg from /test/pic/photo.jpg
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your DocumentRoot corresponds to the portion of the string you want to remove, a solution could be to use
str_replace:But note that you’ll run into troubles in the content of
$_SERVER['DOCUMENT_ROOT']is present somewhere else in your string : it will be removed, each time.If you want to make sure it’s only removed from the beginning of the string, a solution could be to use some regex :
Note the
^at the beginning of the regex (to indicate that it should only match at the beginning of the string) — and don’t forget to escape the special characters from your document root, usingpreg_quote.And to get the name of a file when you have a path containing directory + name, you can use the [`basename`][3] function ; for instance, this portion of code :
Will give you this output :