如何将#sth分配给字符串?

时间:2015-06-01 15:13:08

标签: php string assign

我想创建一种机制来替换$menuitem['url']中的URL链接,例如#link1,#link2,#link3。我花了几个小时搜索路,但我还是很失望。

例如,我需要获取此网址:http://chervo.ideatech.cz/Admin/#link1

而不是当前的:http://chervo.ideatech.cz/Admin/?pg=overview&table=data_obrazky

private function GetHtmlRecursive($arr) {
        $html = '';
        foreach ($arr AS $item) {
        ...
        ...
        ...
            else {
                $html .= '<li><a href="'.$item['url'].'">'.$item['nazev'].'</a></li>';
            }
        }

    return $html;
}



private function FillMenuItem(&$menuitem) {
    ...
    ...
    ...
    else {

        if (isset($menuitem['tab']))
            $this->FillByTabdef($menuitem);

        if ( (!isset($menuitem['url'])) && (isset($menuitem['tab'])) ) {
            $menuitem['url'] = '?pg=overview&amp;table='.$menuitem['tab'];
        }
        ...
        ...
        ...
    }
}

有没有简单的方法,怎么做?

整个代码在这里:http://codepad.org/DvwPJC1V

1 个答案:

答案 0 :(得分:0)

$urls = array('http://chervo.ideatech.cz/Admin/?pg=overview&table=data_obrazky', 'http://chervo.ideatech.cz/Admin/?pg=pickles&table=food', 'http://chervo.ideatech.cz/Admin/?pg=auto&table=cars');

foreach($urls AS $url)
{
    //If http_build_url is available
    //$parse = parse_url($url);
    //unset($parse[PHP_URL_QUERY]);
    //$parse[PHP_URL_FRAGMENT] = "link1";
    //$newURL = http_build_url($parse);
    //var_dump($newURL);

    //otherwise do this

    $query = parse_url($url, PHP_URL_QUERY);

    $newURL = str_replace("?" . $query, "#link1", $url);

    var_dump($newURL);
}

尝试使用http://php.net/manual/en/function.parse-url.php之类的内容来识别要替换的网址部分,然后将其替换为简单的字符串替换或可用http://php.net/manual/en/function.http-build-url.php

或者,您只需删除?字符后的所有内容,然后附加#link1

即可
相关问题