neosmart
neosmart

Reputation: 113

Replace URL with valid ID

I have a PHP String $menu with lots of links. I need to replace the href with an ID based on the link.

I need to

This i what I have:

<a href="http://www.test.de/start/">Link</a>
<a href="http://www.test.de/contact/">Another Link</a>
<a href="http://www.test.de/contact/sub/">Sub Link</a>

And this what I want:

<a href="#start">Link</a> 
<a href="#contact">Another Link</a>
<a href="#contact-sub">Another Link</a>

I tryed it with preg_replace

$search = array(
    "/http:\/\/www.test.de/",
    "/".preg_quote('/">', '/')."/"
);
$replacement = array('#','">');
$menu = preg_replace($search,$replacement,$menu);

My solution looks a little bit "dirty and doesn't replace the Slashes in the middle. Any ideas for a "real" pattern to get this done?

Upvotes: 3

Views: 154

Answers (5)

Marty
Marty

Reputation: 4657

You could use the php function parse_url(); to create an array of the url segments.

ie:

$url = 'http://www.test.de/contact/';
$urlinfo    = parse_url($url);

echo "<h2>URL INFO</h2><pre>";
print_r($urlinfo);
echo "</pre>";
// create the id to be used
$linkID = "#".str_replace('/','',$urlinfo['path']);
echo $linkID;

// OUTPUT
<h2>URL INFO</h2>
Array
(
    [scheme] => http
    [host] => www.test.de
    [path] => /contact/
)
#contact

M

Upvotes: 1

zavg
zavg

Reputation: 11061

  • Change fixed-name domain to #
  • Trim / from the end
  • Replace / with -

    $domain = "http://www.test.de/"
    str_replace('/','-',trim(str_replace($domain, '#', $menu),'/');
    

Upvotes: 0

Lawson
Lawson

Reputation: 634

// Remove the domain
$menu = str_replace("http://www.text.de","",$menu);

// Remove the beginning / and add #
$menu = str_replace("\"/","\"#",$menu);

// Remove the trailing /
$menu = str_replace("/\"","\"",$menu);

// Replace all remaining / with -
$menu = str_replace("/","-",$menu);

Upvotes: 0

amburnside
amburnside

Reputation: 1903

If the domain is always the same try:

str_replace("http://www.test.de/","#", $menu);

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173562

This could be easily done with DOM parsing:

$html = <<<EOM
<a href="http://www.test.de/start/">Link</a>
<a href="http://www.test.de/contact/">Another Link</a>
<a href="http://www.test.de/contact/sub/">Sub Link</a>
EOM;

$dom = new DOMDocument;
$dom->loadHTML($html);

foreach ($dom->getElementsByTagName('a') as $anchor) {
    $href = $anchor->getAttribute('href');
    if (strpos($href, 'http://www.test.de/') === 0) {
        $href = '#' . strtr(trim(parse_url($href, PHP_URL_PATH), '/'), '/', '-');
        $anchor->setAttribute('href', $href);
    }
}

echo $dom->saveHTML();

Demo

Upvotes: 6

Related Questions