im return this url into array
$imagelink = $_SERVER['SERVER_NAME'] ."/test/wp-content/plugins/test/captcha/" .$captchaURL.".jpeg";
when i return $imagelink; in one of my array it shows this :
"Image URL":"localhost/test/wp-content/plugins/test/captcha/LTgLUodmPu.jpeg"
i try to replace the /
$imagelink = $_SERVER['SERVER_NAME'] ."/test/wp-content/plugins/test/captcha/" .$captchaURL.".jpeg";
$replace = str_replace('\/','/',$imagelink);
return $replace;
the result is still the same ? it doesn't seems to replace. why?
its the same as i also use preg_replace() function.
please help if you have the ans.
Use double quotes " while escaping the string
$replace = str_replace("\/","\\",$imagelink);
You are trying to replace forward slashes with forward slashes.
Try either of these:
$imagelink = str_replace('\\', '/', $imagelink); // Replace backslash with forward
$imagelink = str_replace('/', '\\', $imagelink); // Replace forwardslash with backslash
Use the following code:
<?php
$imagelink = $_SERVER['SERVER_NAME'] ."/test/wp-content/plugins/test/captcha/" .$captchaURL.".jpeg";
$replace = str_replace('/','\\',$imagelink);
return $replace;
Related
I have to extract a specific part of an URL.
Example
original URLs
http://www.example.com/PARTiNEED/some/other/stuff
http://www.example.com/PARTiNEED
in case 1 I need to extract
/PARTiNEED/
and in case 2 I need to extract the same part but add an additional "/" at the end
/PARTiNEED/
What I've got right now is this
$tempURL = 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$tempURL = explode('/', $tempURL);
$tempURL = "/" . $tempURL[3] . "/";
is there a more convenient way to do this or is this solution fine?
It's normally a good idea to use PHP's built in functions for things like this where possible. In this case, the parse_url method is designed for parsing URLs.
In your case:
// Extract the path from the URL
$path = parse_url($url, PHP_URL_PATH);
// Separate by forward slashes
$parts = explode('/', $path);
// The part you want is index 1 - the first is an empty string
$result = "/{$parts[1]}/";
You don't need this part:
'http://'. $_SERVER['SERVER_NAME']
you can just do:
$tempURL = explode('/', $_SERVER['REQUEST_URI']);
$tempURL = "/" . $tempURL[1] . "/";
Edited index from 0 to 1 as commented.
Maybe regex suits your needs better?
$tempURL = "http://www.example.com/PARTiNEED/some/other/stuff"; // or $tempURL = "http://www.example.com/PARTiNEED
$pattern = '#(?<=\.com)(.+?)(?=/|$)#';
preg_match($pattern, $tempURL, $match);
$result = $match[0] . "/";
Here this should solve your problem
// check if the var $_SERVER['REQUEST_URI'] is set
if(isset($_SERVER['REQUEST_URI'])) {
// explode by /
$tempURL = explode('/', $_SERVER['REQUEST_URI']);
// what you need in in the array $tempURL;
$WhatUNeed = $tempURL[1];
} else {
$WhatUNeed = '/';
}
Dont worry about the trailing slash, that can be added anytime in your code.
$WhatUNeed = $tempURL[1].'/';
This will give you proper idea about your requirment.
<?php
$url_array = parse_url("http://www.example.com/PARTiNEED/some/other/stuff");
$path = $url_array['path'];
var_dump($path);
?>
now you can use string explode function to get your job done.
I searched "the whole" stackoverflow but didn't find a decent answer that works for me. I need to change the host of a url in php.
This url: http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126
To This: http://example456.com/test?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126
I only need to change the domain and the path or file, so far I've got this:
$originalurl = http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400....
$parts = parse_url($originalurl);
$parts['host'] = $_SERVER['HTTP_HOST'];
$parts['path'] = '/test';
$modifiedurl = http_build_query($parts);
print_r(urldecode($modifiedurl));
but it echos
scheme=http&host=localhost&path=/test&query=t=de&p=9372&pl=bb02799a&cat=&sz=400...
Please I don't want to use some strpos or something like that as I need it to be variable.
Thanks ;)
$url = 'http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126';
$query = parse_url($url)['query'];
$newUrl = 'http://www.younewdomain.com/path?' . $query;
You'll have to do some concatenating manually. This works:
$originalurl = "http://example123.com/query?t=de&p=9372";
$parts = parse_url($originalurl);
$new_path = '/test';
$modifiedurl = $parts['scheme'] . "://" . $_SERVER['HTTP_HOST'] . $new_path . (isset($parts['query']) ? "?".$parts['query']:"");
print_r($modifiedurl);
Came up with a different approach:
$url = "http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126";
$new_host = "http://newhost.com/blab";
//explode at ? so you get the query
$split = explode("?",$url,2);
//build new url
$new_url = $new_host."?".$split[1];
//finish
echo $new_url;
The reverse function of parse_url() should be http_build_url(), have you tried with it?
I have a filename with a date in it, the date is always at the end of the filename.
And there is no extension (because of the basename function i use).
What i have:
$file = '../file_2012-01-02.txt';
$file = basename('$file', '.txt');
$date = preg_replace('PATTERN', '', $file);
Im really not good at regex, so could someone help me with getting the date out of the filename.
Thanks
I suggest to use preg_match instead of preg_replace:
$file = '../file_2012-01-02';
preg_match("/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/", $file, $matches);
echo $matches[1]; // contains '2012-01-02'
If there is always an underscore before the date:
ltrim(strrchr($file, '_'), '_');
^^^^^^^ get the last underscore of the string and the rest of the string after it
^^^^^ remove the underscore
I suggest you to try:
$exploded = explode("_", $filename);
echo $exploded[1] . '<br />'; //prints out 2012-01-02.txt
$exploded_again = explode(".", $exploded[1]);
echo $exploded_again[0]; //prints out 2012-01-02
Shorten it:
$exploded = explode( "_" , str_replace( ".txt", "", $filename ) );
echo $exploded[1];
With this, use regexp when you really need to :
current(explode('.', end(explode('_', $filename))));
This should help i think:
<?php
$file = '../file_2012-01-02.txt';
$file = basename("$file", '.txt');
$date = preg_replace('/(\d{4})-(\d{2})-(\d{2})$/', '', $file);
echo $date; // will output: file_
?>
I'm calling an Amazon API to get a URL to book cover thumbnails.
The thumbnail URL comes back like this:
\\ebc.amazon.com\images\EtcLkasff-_-23.jpg
The forward slashes are the problem. To make these images show up on my page is replace the "\" with a "/". But, my regex php_replace syntax is failing me.
I've got this:
$thumbnail = str_replace('\', '//', $apiString);
I've tried
$thumbnail = str_replace('\\', '//', $apiString);
and it still doesn't work.
Thanks in advance, everyone!!
EDIT:::
after I do:
$thumbnail = "http://" . $thumbnail;
The URL looks like this:
http://\/\/ecx.images-amazon.com\/images\/I\/51vCLCTcmAL._SL75_.jpg
I've tried
$picture4 = str_replace('\', '/', $picture4);
This gives me an escape error.
I'm sorry :( One more edit to make things perfectly clear:
I've done this:
// get amazon picture URL
$picture1 = preg_split('/,/', str_replace("\"", "", $details));
$picture2 = preg_split('/:/', $picture1[7]);
$picture3 = preg_replace('/\\//','/',$picture2[2]);
$picture4 = preg_replace('/\\//','/',$picture3);
// $picture4 = str_replace('\', '/', $picture4);
$picture4 = "http://" . $picture4;
echo "<pre>";print_r($picture4);echo "</pre>";
At this point, $picture4 is:
http://\/\/ecx.images-amazon.com\/images\/I\/51vCLCTcmAL.SL75.jpg
This should really be all you need:
$thumbnail = str_replace('\\', '/', $apiString);
// ^^^ the backslash must be escaped
This would convert:
\\ebc.amazon.com\images\EtcLkasff-_-23.jpg
to
//ebc.amazon.com/images/EtcLkasff-_-23.jpg
If I have a URL that is http://www.example.com/sites/dir/index.html, I would want to extract the word "sites". I know I have to use regular expressions but for some reason my knowledge of them is not working on PHP.
I am trying to use :
$URL = $_SERVER["REQUEST_URI"];
preg_match("%^/(.*)/%", $URL, $matches);
But I must be doing something wrong. I would also like it to have a catch function where if it is at the main site, www.example.com then it would do the word "MAIN"
Edit: sorry, I've known about dirname...It gives the full directory path. I only want the first directory.... So if its www.example.com/1/2/3/4/5/index.html then it returns just 1, not /1/2/3/4/5/
Use the dirname function like this:
$dir = dirname($_SERVER['PHP_SELF']);
$dirs = explode('/', $dir);
echo $dirs[0]; // get first dir
Use parse_url to get the path from $_SERVER['REQUEST_URI'] and then you could get the path segments with explode:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', substr($_SERVER['REQUEST_URI_PATH'], 1));
echo $segments[1];
The dirname function should get you what you need
http://us3.php.net/manual/en/function.dirname.php
<?php
$URL = dirname($_SERVER["REQUEST_URI"]);
?>
Just wanted to recommend additionally to check for a prefixed "/" or "\"
and to use DIRECTORY_SEPARATOR :
$testPath = dirname(__FILE__);
$_testPath = (substr($testPath,0,1)==DIRECTORY_SEPARATOR) ? substr($testPath,1):$testPath;
$firstDirectory = reset( explode(DIRECTORY_SEPARATOR, dirname($_testPath)) );
echo $firstDirectory;
A simple and robust way is:
$currentWebDir = substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT']));
If you are worried about DIRECTORY_SEPARATORS, you could also do:
$currentWebDir = str_replace('\\', '/', substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT'])));
Also be aware of mod_rewrite issues mentioned by FrancescoMM