I'm trying to edit a wordpress permalink like the one here...
echo get_permalink();
This permalink as it is will output...
domain.com/directory/mycustompage/
I'm looking for a way using str_replace() to make the the URL to become...
domain.com/NEWDIRECTORY/?draft=mycustompage
So as you can see I want to change the middle directory to "NEWDIRECTORY" and also grab the last directory name "mycustompage" and use that as the parameter.
I apologise if this seems easy, I've just started seriously coding with PHP this year at school.
Thanks
This should do what you're after. I've put the URL in a variable and then exploded it and rebuilt it.
<?php
$url = "domain.com/directory/mycustompage/";
$exploded = explode('/', $url);
$newURL = $exploded[0].'/NEWDIRECTORY/?draft='.$exploded[2];
echo $newURL;
?>
Outputs domain.com/NEWDIRECTORY/?draft=mycustompage as requested
Here's what I have and works! thanks for pointing me in the right direction!
$prev = get_permalink($prevID);
$prev = explode('/', $prev);
$prev = array_filter($prev);
$prev = array_merge($prev, array());
$prev = preg_replace('/\?.*/', '', $prev);
$prev3 = $prev[3];
I then reconstruct the URL.
Related
I've a PHP file. In this file I need to check, if my URL has the following ending:
www.example.de/dashboard/2/
So the ending can be a number 1 - 99+ which is always at the end of the url between two slashes. I can't use $_GET here. If it is $_GET, it would be easy:
if ( isset($_GET['ending']) ) :
So how can I do this without a parameter in the URL? Thanks for your help!
if(preg_match('^\/dashboard\/(\d+)', $_SERVER['REQUEST_URI'])){
foo();
}
Use regular expression on the request uri
You can make use of parse_url and explode:
$url = 'http://www.example.de/dashboard/2/';
$path = parse_url($url, PHP_URL_PATH); // '/dashboard/2/'
$parts = explode('/', $path); // ['', 'dashboard', '2', '']
$section = $parts[1]; // 'dashboard'
$ending = $parts[2]; // '2'
Demo: https://3v4l.org/dv6Cn
You can also make use of URL rewriting (this is for a Apache-based web server, but you can find simular resources for nginx or any other web servers if need be).
A more dynamic way is to explode and use array_filter to remove empty values then pick the last item.
If the item * 1 is the same as the item then we know it's a number.
(The return from explode is strings so we cant use is_int)
$url = "http://www.example.de/dashboard/2/";
$parts = array_filter(explode("/", $url));
$ending = end($parts);
if($ending*1 == $ending) echo $ending; //2
First you need to target this url to script - in web server config. For nginx and index.php:
try_files $uri #rewrite_location;
location #rewrite_location {
rewrite ^/(.*) /index.php?link=$1&$args last;
}
Second - you need to parse URI. In $end you find what you want
$link_as_array = array_values(array_diff(explode("/", $url), array('')));
$max = count($link_as_array) - 1;
$end = $link_as_array[$max];
I would think this way. If the URL is always the same, or the same format, I'll do the following:
Check for the approx URL.
Split the URL into pieces.
Find if there's the part I am looking for.
Extract the number.
<?php
$url = "http://www.example.de/dashboard/2/";
if (strpos($url, "www.example.de/dashboard") === 7 or strpos($url, "www.example.de/dashboard") === 8) {
$urlParts = explode("/", $url);
if (isset($urlParts[4]) && isNumeric($urlParts[4]))
echo "Yes! It is {$urlParts[4]}.";
}
?>
The strpos with 7 and 8 is for URL with http:// or https://.
The above will give you the output as the numeric part if it is set. I hope this works out.
I am tryign to remove pagination page ID from URL. For example I have URL looks like this:
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
And want to remove, If any of matches match from URL:
1. page=1
2. page=1&page=2
3. page=1&page=2$page=3
4. page=1&page=2$page=3$page=4
In my current pagination code, Previous page is concating everytime when page is changed thats why I want to remove all page.
I have used this code but not working well.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
//$getUrl =$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$parts = parse_url($urlVal);
$urlVar = "";
$urlVar = $parts['query'];
echo $urlVar = preg_replace('/page=[0-9]&+/', '', $urlVar);
Try my code here: http://codepad.org/lSHV7a7w
I hope you understand what I am trying to do.
Thanks.
Try this.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
$parts = parse_url($urlVal);
parse_str($parts['query'], $query);
unset($query['page']);
$newUrl = "{$parts['scheme']}://{$parts['host']}{$parts['path']}?" . http_build_query($query);
echo $newUrl;
// Regx Pattern
echo preg_replace('/&?page=[0-9]+&?/', '', $parts['query']);
How about using concatenation of numbers as page value?
$urlVal = "http://192.168.1.233/sitename/property-list?page=11,13&etc..."
It shortens url and makes removing parameter easy:
$urlVar = preg_replace('/page=[0-9,]+&/', '', $urlVar);
You can extract them from url using:
preg_match("/page=([0-9,]+)&/", $urlVal, $m);
$pages = explode(",", $m[1]);
I have looked around for this but can only find links and references to this been done after an anchor hashtag but I need to get the value of the URL after the last / sign.
I have seen this used like this:
www.somesite.com/archive/some-post-or-article/53272
the last bit 53272 is a reference to an affiliate ID..
Thanks in advance folks.
PHPs parse_url (which extracts the path from the URL) combined with basename (which returns the last part) will solve this:
var_dump(basename(parse_url('http://www.somesite.com/archive/some-post-or-article/53272', PHP_URL_PATH)));
string(5) "53272"
You can do this :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$id = substr(url, strrpos(url, '/') + 1);
You can do it in one line with explode() and array_pop() :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
echo array_pop(explode('/',$url)); //echoes 53272
<?php
$url = "www.somesite.com/archive/some-post-or-article/53272";
$last = end(explode("/",$url));
echo $last;
?>
Use this.
I'm not an expert in PHP, but I would go for using the split function: http://php.net/manual/en/function.split.php
Use it to split a String representation of your URL with the '/' pattern, and it will return you an array of strings. You will be looking for the last element in the array.
This will work!
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$pieces = explode("/", $url);
$id = $pieces[count($pieces)]; //or $id = $pieces[count($pieces) - 1];
If you always have the id on the same place, and the actual link looks something like
http://www.somesite.com/archive/article-post-id/74355
$link = "http://www.somesite.com/archive/article-post-id/74355";
$string = explode('article-post-id/', $link);
$string[1]; // This is your id of the article :)
Hope it helped :)
$info = parse_url($yourUrl);
$result = '';
if( !empty($info['path']) )
{
$result = end(explode('/', $info['path']));
}
return $result;
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$parse = explode('/',$url);
$count = count($parse);
$yourValue = $parse[$count-1];
That's all.
I have the following URL that is being returned to me from the Vzaar upload api:
https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"
I need to be able to strip just the first "vz82b51a36989c422abc4db1734208933a" out of the url. Is there any way to do this in PHP?
Thank you in advance for any help that you provide.
That string seems to appear twice in your URL (is it even a valid URL?). You also did not define any rules for obtaining it, so here I just take the 5th "part" separated by slashes.
<?php
$url = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"';
$parsed = parse_url($url);
$parts = explode('/', $parsed['path']);
echo $parts[5];
?>
What you do is take the string right after the "source" tag. Done by exploding.
$a = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f';
$parts = explode('/', $a);
$i = 0;
for(; $i < count($parts); $i++)
if($parts[$i] == 'source')
break;
$i++;
echo $parts[$i];
If it's always between slashes, you can explode() on / and grab the n-th item.
Try this:
$str = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"';
$str = substr($str, strpos($str, 'source/') + 7);
echo substr($str, 0, strpos($str, '/'));
At this example, the position of 'source' in the URL is not necessary.
Is the string always at the exact same location? if so, how about something like...
<?php
$url = 'https://vz1.s3.amazonaws.com/vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4vz1vzaar/vz8/2b5/source/vz82b51a36989c422abc4db1734208933a/test.mp4"c9f4852682649c4a1c034af092b2938f"';
$url = parse_url($url);
$url = explode('/',$url['path']);
print_r($url[5]);
?>
The above code would print vz82b51a36989c422abc4db1734208933a
I know I'm late to the party here, but you shouldn't need to do that
The GUID will be returned to you in the XML response from the first step in the upload process.
http://developer.vzaar.com/docs/version_1.0/uploading/sign.html
See the example at the bottom, you can pull the GUId straight from there.
<?xml version="1.0" encoding="UTF-8"?>
<vzaar-api>
<guid>vz7651d8c2558b46179531548224c87f84</guid>
<key>vz7/651/source/vz7651d8c2558b46179531548224c87f84/${filename}</key>
<https>false</https>
<acl>private</acl>
<bucket>vzaar_development_bucket</bucket>
<policy>ewogICAgICAnZ ... JywgIF0KICAgICAgfQ==</policy>
<expirationdate>2009-06-11T00:05:43.000Z</expirationdate>
<accesskeyid>96ZODEDA709P5JNKI6X08U7PBQ31GUY8</accesskeyid>
<signature>1ZwSGQjv4nrKUM1M/euO8FdxG20=</signature>
</vzaar-api>
Having a brain freeze...
Have a URL which may be in any of the formats :
http://url.com/stuff
url.com/somestuff
www.url.com/otherstuff
https://www.url.com/morestuff
You get the picture.
How do I remove the .com part to leave just the various 'stuff' parts ? For example, the above would end up :
stuff
somestuff
otherstuff
morestuff
You could achieve that using the following code:
$com_pos = strpos($url, '.com/');
$stuff_part = substr($url, $com_pos + 5);
Click here to see the working code.
This should do the trick for you!
<?php
$url = "http://url.com/stuff";
$querystring = preg_replace('#^(https|http)?(://)?(www.)?([a-zA-Z0-9-]+)\.[a-zA-Z]{2,6}/#', "", $url);
echo $querystring;
I submitted this answer because I'm not very fond of solutions using explode() to handle this. Maybe your query string contains more slashes so, you'd have to write exceptions for those cases.
You can use explode to make an array, then get the last element from the array.
$str = 'http://url.com/stuff';
$arr = explode('/', $str);
echo end($arr); // 'stuff'
$path = parse_url('http://url.com/stuff', PHP_URL_PATH);
If you leave the second parameter unspecified you can return an array including the domain etc.
Use explode function to divide the string.
<?php
$url = "http://url.com/stuff";
$stuff = explode("/", $url);
echo $stuff[sizeof($stuff) - 1];
?>
I used sizeof to access to last element.
preg_replace("/^(https?:\/\/)?[^\/]+/" ,"", $url);