doushou9028
2015-10-13 22:20
浏览 416
已采纳

PHP用str_replace()替换URL段;

I have "/foo/bar/url/" coming straight after my domain name.

What I want is to find penultimate slash symbol in my string and replace it with slash symbol + hashtag. Like so: from / to /# (The problem is not how to get URL, but how to handle it)

How this could be achieved? What is the best practice for doing stuff like that? At the moment I'm pretty sure that I should use str_replace();

UPD. I think preg_replace() would be suitable for my case. But then there is another problem: what should regexp look like in order to make my issue solved?

P.S. Just in a case I'm using SilverStripe framework (v3.1.12)

图片转代码服务由CSDN问答提供 功能建议

我的域名后面有“/ foo / bar / url /”

我想要的是在我的字符串中找到倒数第二个斜杠符号,并用斜杠符号+ hashtag替换它。 像这样:从 / /#(问题不在于如何获取URL,而是如何处理它)

如何 这可以实现吗? 做这样的事情的最佳做法是什么? 目前我非常确定我应该使用str_replace();

UPD。 我认为 preg_replace()适合我的情况。 但是还有另一个问题:为了让我的问题得到解决,regexp应该是什么样的?

P.S。 在我正在使用 SilverStripe 框架( v3.1.12 的情况下

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dseslyh6662605 2015-10-13 22:33
    已采纳
    $url = '/foo/bar/url/';
    
    if (false !== $last = strrpos($url, '/')) {
        if (false !== $penultimate = strrpos($url, '/', $last - strlen($url) - 1)) {
            $url = substr_replace($url, '/#', $penultimate, 1);
        }
    }
    
    echo $url;
    

    This will output

    /foo/bar/#url/
    

    If you want to strip the last /:

    echo rtrim($url, '/'); // print /foo/bar/#url
    
    已采纳该答案
    打赏 评论
  • dqs86517 2015-10-13 22:33

    Here is a method that would function. There are probably cleaner ways.

    // Let's assume you already have $url_string populated
    $url_string = "http://whatever.com/foo/bar/url/";
    
    $url_explode = explode("\\",$url_string);
    $portion_count = count($url_explode);
    
    $affected_portion = $portion_count - 2; // Minus two because array index starts at 0 and also we want the second to last occurence
    
    $i = 0;
    $output = "";
    foreach ($url_explode as $portion){
    
        $output.=$portion;
    
        if ($i == $affected_portion){
            $output.= "#";
        }
    
        $i++;
    
    }
    
    $new_url = $output;
    
    打赏 评论
  • doudeng3008 2015-10-13 22:55

    In my case this solved my problem:

    $url = $this->Link();
    $url = rtrim($url, '/');
    $url = substr_replace($url, '#', strrpos($url, '/') + 1, 0);
    
    打赏 评论
  • dsf4354353452 2015-10-14 05:48

    Assuming you now have

    $url = $this->Link(); // e.g. /foo/bar/my-urlsegment
    

    You can combine it like

    $handledUrl = $this->ParentID 
        ? $this->Parent()->Link() + '#' + $this->URLSegment
        : $this->Link();
    

    where $this->Parent()->Link() is e.g. /foo/bar and $this->URLSegment is my-urlsegment

    $this->ParentID also checks if we have a parent page or are on the top level of SiteTree

    打赏 评论
  • dotdx80642 2015-10-16 05:50

    I might be tooooo late for answering this question but I thought this might help you. You can simply use preg_replace like as

    $url = '/foo/bar/url/';
    echo preg_replace('~(\/)(\w+)\/$~',"$1#$2",$url);
    

    Output:

    /foo/bar/#url
    
    打赏 评论

相关推荐 更多相似问题