2013-07-10 60 views
1

我试图做到这一点,有很多的问题想出来的。我知道我是一个完整的PHP noob,但我真的需要弄清楚,所以我会感谢您的帮助。试图获得一个相对路径

我想传递一个“修正”的网址在我的网站一个小部件,以便在用户点击一个按钮,他们会下载他们在PDF页面。

我的网站使用友好的URL http://www.sample.com/friendly/url/this/thing.html

什么,我需要做的就是抓住当前URL并更改友好的PDF格式,并把结果到按钮。

我想出处于#shitsandwich

<?php 
function curPageURL() { 
$isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on"); 
$port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] !=   "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443"))); 
$port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : ''; 
$url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"]; 
return $url; 
} 

echo curPageURL(); 
?> 

导致我的网址上#filetofpain #failsauce毛毛雨。

我还没有得到的URL部分的替换部分,因为我无法弄清楚如何获得URL或打破它。这是可悲的。

因此,像用锡杯和抹布上我一个乞丐恳求帮助:)

我需要做的是打破了URL返回成片什么......

如果它返回 “ www.mysite.com/01/05/2013/9013/your-mom-wears-combat-boots/“ 我需要将其更改为: ” www.mysite.com/pdf/9013/your-mom-wears -combat-boots.pdf”

(可能以http://在它前面的还有这里的发布机制的东西我张贴的链接和我没有信誉,因此禾不能让我把全部的http:在实例// WWW mysite的COM)

+2

你的错误是两个'echo's彼此相邻。 – apartridge

回答

3

您正在使用双echo echo $current_url;变化echo $current_url;

+0

对不起Niloy我正在编辑原始帖子......现在完成了。 echo $ current_url并不是问题,就像搞清楚如何分解它并更改url并将其形成一个声明:( – Casca

0

您可以使用$_SERVER["HTTP_HOST"]$_SERVER["REQUEST_URI"]获得请求的URL。

0

我没试过你的代码,但它似乎已经有一个双echo线..

无论如何,我想给你一个非常简单的方法,我喜欢用:

function curPageURL() { 
    $pageURL = 'http'; 
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} 
    $pageURL .= "://"; 
    if ($_SERVER["SERVER_PORT"] != "80") { 
     $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
    } 
    else { 
     $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 
    } 
    return $pageURL; 
} 

这个功能的作用是每当你打电话给curPageURL()时,你就会得到你当前的网页网址,就像它在网址栏里看到的一样。比你可以随心所欲地工作。

我希望这能解决你的问题:)

编辑:

要使用该用于获取URL和比对了变化,从.html.pdf所有你需要做的结局:

$url = curPageURL(); // gets back www.sample.com/page.html 
$newurl = str_replace(".html", ".pdf", $url); // changes url to www.sample.com/page.pdf 

该代码使用顶级函数

编辑2:

如果返回你:www.mysite.com/01/05/2013/9013/your-mom-wears-combat-boots/ 比你需要做的:

$url = curPageURL(); // gets back www.sample.com/page/ 
$newurl = substr_replace($url, "", -1) // changes url to www.sample.com/page 
$newurl .= ".pdf" // adds the .pdf to the url so you get www.sample.com/page.pdf 

编辑3:

首先的 - 很高兴你使用WordPress :)

现在,wordpress codex使得它更容易,实际上更容易做到: 为此我们需要进行一些更改,同时更改curPageURL函数。

function curPageURL() { 
    $pageURL = 'http'; 
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} 
    $pageURL .= "://"; 
    if ($_SERVER["SERVER_PORT"] != "80") { 
     $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]; 
    } 
    else { 
     $pageURL .= $_SERVER["SERVER_NAME"]; 
    } 
    return $pageURL; 
} 

,我会解释:
$_SERVER["SERVER_NAME"]给我们的域名,如domain.com
$_SERVER["REQUEST_URI"]向我们返回域,路径后的所有内容。在我们的例子中为 ,例如:01/03/2013/9013/page
所以现在curPageURL函数只返回域名。

,现在让我们得到什么是未来的工作:

$url = curPageURL(); // gets back http://www.sample.com 
$url .= "/pdf"; // we add the /pdf to the url so it is now: http://www.sample.com/pdf 
$postid = get_the_ID(); // gives us the post ID. for example 9013 
$url .= "/" . $postid; // now we take the post ID and add it to the url: http://www.sample.com/pdf/9013 
$the_post = get_post($postid); // get the post by the ID 
$post_slug = $the_post['post_name']; // get the slug of the post 
$url .= "/" . $post_slug; // adds the slug to the url: http://www.sample.com/pdf/9013/your_mom_wears_combat_boots 
$url .= ".pdf"; // finally add the pdf to the url so now you get: http://www.sample.com/pdf/9013/your_mom_wears_combat_boots.pdf 

这里是没有注释:

$url = curPageURL(); 
$url .= "/pdf"; 
$postid = get_the_ID(); 
$url .= "/" . $postid; 
$the_post = get_post($postid); 
$post_slug = $the_post['post_name']; 
$url .= "/" . $post_slug; 
$url .= ".pdf"; 

,如果你想将线最小化:

$url = curPageURL() . "/pdf/" . get_the_ID(); // url is now: www.domain.com/pdf/postid 
$the_post = get_post(get_the_ID()); 
$url .= "/" . $the_post['post_name'] . ".pdf"; //url is now: www.domain.com/pdf/postid/post_slug.pdf 

现在你可以echo该网址通过echo $url;

更新,最后编辑(4):

我犯了一个错误,有关获取蛞蝓。
我用过:$the_post = get_post(get_the_ID());而且拿了slu,,名字:$the_post['post_name']

这是不对的。

采取蛞蝓是最好的出路是通过运行代码:
$post_slug = basename(get_permalink());

所以这chagnes一点点它supposd寻找方式:

$url = curPageURL() . "/pdf/" . get_the_ID(); // url is now: www.domain.com/pdf/postid 
$post_slug = basename(get_permalink()); //gets the post slug 
$url .= "/" . $post_slug . ".pdf"; //url is now: www.domain.com/pdf/postid/post_slug.pdf 

,我们可以把它越来越最小化:

$url = curPageURL() ."/pdf/". get_the_ID() ."/". basename(get_permalink()) .".pdf"; 

,现在你可以echo $url;

请不要忘记:此功能需要位于page.phpsingle.php中,才能知道帖子的内容和帖子的ID。
行:$url = curPageURL() ."/pdf/". get_the_ID() ."/". basename(get_permalink()) .".pdf";需要在循环:<?php if (have_posts()) : while (have_posts()) : the_post(); ?>或您使用的任何其他循环。

这应该做的工作:)

+0

是否-1取下一个部分?例如/ 01/05/the - 1会使它/ 01 /?我需要9013部分和yourmomwears部分,但我需要在此之前插入pdf ...所以原始URL如下所示:www.blah.com/01/03/2013/9013/page /到www.blah.com/pdf/9013/page.pdf,所以我想我需要更多地了解substr_replace实际上到达那里的情况,但我不清楚如何将pdf放在字符串中间:(对不起,我很慢,我觉得我应该更快地选择它。 – Casca

+0

我使用'substr_replace'来删除最后一个'/',所以我改变了:'www.blah.com/01/03/2013/9013/page /'到'www.blah.com/01/03/2013/9013/page',然后我添加pdf的完成,就像你之前问过的那样。我知道的是你想要的是不同的为你提供你正在寻找的解决方案或者我需要知道网站中所有链接的共同之处。所以我会知道如何为你做。另一件事是 - 这是wordpress吗?链接肯定看起来像一个常见的WordPress的链接:)这样做会更容易做! –

+0

谢谢:) - 是的,它是wordpress。所有的网址都像是网站名称,日期,postid和seo友好名称。 例如: HTTP:// WWW样品点/月/日/年/帖子ID/your_mom_wears_combat_boots 我需要的是删除月,日,年,在那个位置添加PDF,然后删除最后一个尾随/并把.PDF 所以结果应该是这样的: HTTP://WWW的样品点/ PDF /帖子ID/your_mom_wears_combat_boots.pdf – Casca