I am creating a PHP proxy where it accepts a url and confirms it is on my list of servers.
When importing the url from the application i ran it to an issue where i needed 2 parser tags. i need it to split along a "\?" tag as well as a string, in my case, "export?"
i am using preg for the first tag. Does this accept the strings like my export tag or is there some other method for doing this?
please le me know how this is accomplished or if you have more questions.
As ircmaxell has already stated in the comments, PHP does already have a function to parse a URL: parse_url.
And when you have the URL path (I assume your export? the path suffix plus the query indicator), you can use explode to split the path into its path segments:
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', $path);
You can then get the last path segment with one of the following:
end($segments)
$segments[count($segments)-1]
And to cope with trailing slashes, you can use rtrim($path, '/') to remove them.
All together:
$url = 'http://www.example.com/subfolders/export?';
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', rtrim($path, '/'));
echo end($segments);
A regular expression should do the trick, something like the below would work. This is what Django uses in their URL dispatcher
r'^export/$'
Regular expressions are strings matches that may also include variable matches. Because ? is included within ?, you have to do your split twice. Once on export? first, and a second pass on each of those with ? as your delimiter. As written below, you're just splitting on either of two different strings.
$first = preg_split('export\?', ...);
for ($first) {
array_push ($second,preg_split('\?', ...)');
}
That isn't perfectly valid PHP, but I hope it is close enough pseudocode.
Hey guys i ended up using an explode which looked for the string (export?) and then i used the preg split command to search for the \?. this provided me with the protion i was looking for. thanks guys.
Related
The last question was marked as a duplicate so I'm reopening since $_SERVER['REQUEST_URI']: isnt what I'm looking for because it displays the entire path.
I need to just display the name of the 2nd folder alone without the path, without forward slashes and without the pagename
Here is the structure of the URL:
http://example.com/sub/THISFOLDER/page.php
the domain will change, so I'm looking for a solution that will work for any domain as long as it targets the 2nd folder.
What I want to do is something like this:
if THISFOLDER is named folder1 then { include("header2.php"); }
To fetch the current folder name use this method:
$arr = explode('/', dirname(__FILE__));
$whatyouneed = $arr[count($arr)-1];
<?php
$str = 'http://example.com/sub/THISFOLDER/page.php';
$parts = parse_url($str);
$folders = explode('/', $parts['path']);
var_dump($folders[2]);
Output:
string(10) "THISFOLDER"
I used parse_url so it will work easily regardless of the exact url structure.
If you always want to get the last folder before the php page. (Even if it is not the second you can use this code).
<?php
$thisPath = $_SERVER['PHP_SELF'];;
$pattern = '/(\W+)\/(\w+)\/(\w+)/';
$replacement = '$2';
echo preg_replace($pattern, $replacement, $string);
?>
Sorry, I don't have a php instance spun up to actually test this, but the way it should work is this:
Looking at it from back to front:
It will find normal word characters and hit the slash, that is accounted for by "/". Then it will look for more normal characters. The '/' is covered, then it will look for any possible non-white space character to cover the rest. You want the middle portion.
I have a page at:
http://somewebsite.com/1234/test/
How do I get the 1234 extracted from it with a PHP regex?
Don't use RegEx use parse_url() and explode().
For one level up, use dirname()
I like to avoid regex if I don't need it, so I would recommend you do it this way:
$url = explode("/", $_SERVER['PHP_SELF']);
Then you can reference the part of the url that you want using this:
$url[1]
If you need/want to use regex, I'll think about it for a second and try to post a solution later.
To just get the one above you can use
echo dirname("http://www.google.com/cake/lol");
Outputs
http://www.google.com/cake
Or for just the bit between the / and / you could do
var_dump(explode("/", "http://www.google.com/cake/lol"));
Or in regex
preg_match_all('#/([^/]*)/#',$sourcestring,$matches);
Perform regex on $_SCRIPT['REQUEST_URL'] or split it by '/' and get the first element => 1234
If the PHP was triggered by launching that URL, then these will probably be true:
$_SERVER['REQUEST_URI'] == "/1234/test/"
dirname($_SERVER['REQUEST_URI']) == "/1234"
basename(dirname($_SERVER['REQUEST_URI'])) == "1234"
preg_match(".*\/(\w+)\/(\w+)\/", "$url", $matches);
The parent directory is $matches[1].
I have the following url. http://domain.com/userfiles/dynamic/images/whatever_dollar_1318105152.png
Everything in the url can change except the userfiles part and the last underscore. Basically I want to get the part of the url which is userfiles/dynamic/images/whatever_dollar_ What is a good way to do this. I'm open or both JavaScript or php.
Use parse_url in PHP to split an url in its various parts. Get the path part that is returned. It contains the path without the domain and the query string.
After that use strrpos to find the last occurrance of the _ within the path.
With substr you can copy the first part of the path (up until the found _) and you're done.
You could, with JavaScript, try:
var string = "http://domain.com/userfiles/dynamic/images/whatever_dollar_1318105152.png";
var newString = string.substring(string.indexOf('userfiles'),string.lastIndexOf('_'));
alert(newString); // returns: "userfiles/dynamic/images/whatever_dollar" (Without quotes).
JS Fiddle demo.
References:
substring().
indexOf().
lastIndexOf().
Assuming your string is stored in $s, simply:
echo preg_replace('/.*(userfiles.*_).*/', '$1', $s);
I’m working on a small hoppy project where I want to replace a specific page on a URL. Let me explain:
I’ve got the URL
http://www.example.com/article/paragraph/low/
I want to keep the URL but replace the last segment /low/ with /high/ so the new URL is:
http://www.example.com/article/paragraph/high/
I’ve tried different explode, split and splice but I just can’t seem to wrap my head around it and make it work. I can change the entire URL but not just the last segment and save it in a new variable.
I’m pretty confidence that it is a pretty straight forward case but I’ve never worked that much with arrays / string-manipulation in PHP so I’m pretty lost.
I guess that I have to first split the URL up in segments, using the "\" to separate it (I tried that but have problems by using explode("\", $string)) and then replace the last \low\ with \high\
Hope someone could help or point me in the right direction to what methods to use for doing this.
Sincere
Mestika
how about str_replace?
<?php
$newurl = str_replace('low', 'high', $oldurl);
?>
documentation;
http://php.net/manual/en/function.str-replace.php
edit;
Rik is right; if your domain (or any other part of the url for that matter) includes the string "low", this will mess up your link.
So: if your url may contain multiple 'low' 's, you will have to add an extra indicator in the script. An example of that would be including the /'s in your str_replace.
You took \ for /.
$url = explode('/', rtrim($url, '/'));
if (end($url) == 'low') {
$url[count($url)-1] = 'high';
}
$url = implode('/', $url) .'/';
Use parse_url to split the URL into its components, modify them as required (here you can use explode to split the path into its segments), and then rebuild the URL with http_build_url.
<?php
class TestURL extends PHPUnit_Framework_TestCase {
public function testURL() {
$URL = 'http://www.mydomain.com/article/paragraph/low/';
$explode = explode('/', $URL);
$explode[5] = 'high';
$expected = 'http://www.mydomain.com/article/paragraph/high/';
$actual = implode('/', $explode);
$this->assertEquals($expected, $actual);
}
}
--
phpunit simple-test.php
PHPUnit 3.4.13 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 4.75Mb
OK (1 test, 1 assertion)
This will probably be enough:
$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = str_replace('/low/', '/high/', $url);
or with regular expressions (it allows more flexibility)
$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = preg_replace('/low(\/?)$/', 'high$1', $url);
Note that the string approach will replace any low segment and only if it's followed by a /. The regex approach will replace low only if it's the last segment and it may not be followed by a /.
I am trying to get the page or last directory name from a url
for example if the url is: http://www.example.com/dir/ i want it to return dir or if the passed url is http://www.example.com/page.php I want it to return page Notice I do not want the trailing slash or file extension.
I tried this:
$regex = "/.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*/i";
$name = strtolower(preg_replace($regex,"$2",$url));
I ran this regex in PHP and it returned nothing. (however I tested the same regex in ActionScript and it worked!)
So what am I doing wrong here, how do I get what I want?
Thanks!!!
Don't use / as the regex delimiter if it also contains slashes. Try this:
$regex = "#^.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*$#i";
You may try tho escape the "/" in the middle. That simply closes your regex. So this may work:
$regex = "/.*\.(com|gov|org|net|mil|edu)\/([a-z_\-]+).*/i";
You may also make the regex somewhat more general, but that's another problem.
You can use this
array_pop(explode('/', $url));
Then apply a simple regex to remove any file extension
Assuming you want to match the entire address after the domain portion:
$regex = "%://[^/]+/([^?#]+)%i";
The above assumes a URL of the format extension://domainpart/everythingelse.
Then again, it seems that the problem here isn't that your RegEx isn't powerful enough, just mistyped (closing delimiter in the middle of the string). I'll leave this up for posterity, but I strongly recommend you check out PHP's parse_url() method.
This should adequately deliver:
substr($s = basename($_SERVER['REQUEST_URI']), 0, strrpos($s,'.') ?: strlen($s))
But this is better:
preg_replace('/[#\.\?].*/','',basename($path));
Although, your example is short, so I cannot tell if you want to preserve the entire path or just the last element of it. The preceding example will only preserve the last piece, but this should save the whole path while being generic enough to work with just about anything that can be thrown at you:
preg_replace('~(?:/$|[#\.\?].*)~','',substr(parse_url($path, PHP_URL_PATH),1));
As much as I personally love using regular expressions, more 'crude' (for want of a better word) string functions might be a good alternative for you. The snippet below uses sscanf to parse the path part of the URL for the first bunch of letters.
$url = "http://www.example.com/page.php";
$path = parse_url($url, PHP_URL_PATH);
sscanf($path, '/%[a-z]', $part);
// $part = "page";
This expression:
(?<=^[^:]+://[^.]+(?:\.[^.]+)*/)[^/]*(?=\.[^.]+$|/$)
Gives the following results:
http://www.example.com/dir/ dir
http://www.example.com/foo/dir/ dir
http://www.example.com/page.php page
http://www.example.com/foo/page.php page
Apologies in advance if this is not valid PHP regex - I tested it using RegexBuddy.
Save yourself the regular expression and make PHP's other functions feel more loved.
$url = "http://www.example.com/page.php";
$filename = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);
Warning: for PHP 5.2 and up.