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);
Related
I need to parse the id from the following string:
https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2
I need to only return the following:
id1161503945
The string always begins with https://itunes.apple.com/ and ends with ?i=#####&uo=2
I tried string and replace with wildcards but that did not work.
Well, you can use this below regex. It is working. I have use preg_replace function.
$data = 'https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2';
echo preg_replace("/(.*)\/(\w+)\?(.*)/","$2",$data);
Output is
id1161503945
Or You can use
preg_match("/(\/)(\w+)(\?)/",$data,$m);
echo $m[2];
Same output.
Hope it help you
If it's really always the last element (before query params) in the url, then you can use this simple regex:
'/id[^?]+/'
CAUTION: as pointed by #xhienne, this works only if you're sure that another id string doesn't appear anywhere before the searched part.
If it may happen, rather use:
'/id[\d]+/'
This way, it's safe with respect to a previous id string, but the searched id must be followed by digits only.
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.
What I would like to do is remove the "http://" part of these autogenerated links, below is an example of it.
http://google.com/search?gc...
Here are the regexes I am using in PHP to generate these links from a URL.
$patterns_sp[5] = '~([\S]+)~';
$replaces_sp[5] = '<a href=\1 target="_blank">\1<br/>';
$patterns_sp[6] = '~(?<=\>)([\S]{1,25})[^\s]+~';
$replaces_sp[6] = '\1...</a><br/>';
When these patterns are run on a URL like this:
http://www.google.com/search?gcx=c&ix=c1&sourceid=chrome&ie=UTF-8&q=regex
the REGEX gives me:
http://google.com/search?gc...
Where I am stuck:
There is no obvious reason why I cannot modify the fourth line of code to read like this:
$patterns_sp[6] = '~(?<=\>http\:\/\/)([\S]{1,25})[^\s]+~';
However, the REGEX still seems to capture the "http://" part of the address, thus making a long list of these very redundant looking. What I am left with is the same thing as in the first example.
Replace...
$patterns_sp[5] = '~([\S]+)~';
...with...
$patterns_sp[5] = '~^(?:https?|ftp):([\S]+)~';
Then you can access the protocol-less version with $1 and the whole link with $0.
Optionally, you can remove a leading protocol with something like...
preg_replace('/^(?:https?|ftp):/', '', $str);
I suggest not writing your own regex, instead have a look at http://php.net/manual/en/function.parse-url.php
Retrieve the components of the URL, then compose a new version that only contains the parts you want.
I am very confused about the following:
echo("<a href='http://".urlencode("www.test.com/test.php?x=1&y=2")."'>test</a><br>");
echo("<a href='http://"."www.test.com/test.php?x=1&y=2"."'>test</a>");
The first link gets a trailing slash added (that's causing me problems)
The second link does not.
Can anyone help me to understand why.
Clearly it appears to be something to do with urlencode, but I can't find out what.
Thanks
c
You should not be using urlencode() to echo URLs, unless they contain some non standard characters.
The example provided doesn't contain anything unusual.
Example
$query = 'hello how are you?';
echo 'http://example.com/?q=' . urlencode($query);
// Ouputs http://example.com/?q=hello+how+are+you%3F
See I used it because the $query variable may contain spaces, question marks, etc. I can not use the question mark because it denotes the start of a query string, e.g. index.php?page=1.
In fact, that example would be better off just being output rather than echo'd.
Also, when I tried your example code, I did not get a traling slash, in fact I got
<a href='http://www.test.com%2Ftest.php%3Fx%3D1%26y%3D2'>test</a>
string urlencode ( string $str )
This function is convenient when
encoding a string to be used in a
query part of a URL, as a convenient
way to pass variables to the next
page.
Your urlencode is not used properly in your case.
Plus, echo don't usually come with () it should be echo "<a href='http [...]</a>";
You should use urlencode() for parameters only! Example:
echo 'http://example.com/index.php?some_link='.urlencode('some value containing special chars like whitespace');
You can use this to pass URLs, etc. to your URL.
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.