3

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

  • remove the domain
  • remove Slashes at the begining and the end
  • replace Slashes in the middle with '-'

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?

Vinicius Garcia
  • 1,740
  • 4
  • 30
  • 54
neosmart
  • 103
  • 8

5 Answers5

6

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

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • +1 for the most robust solution (regexp for HTML parsing is BAD: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags ) – LSerni Apr 17 '13 at 13:41
  • Yes, the domain is always the same.Thanks for your great tipps - I will try it immediately. – neosmart Apr 17 '13 at 14:38
1

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

Marty
  • 4,619
  • 2
  • 24
  • 35
0

If the domain is always the same try:

str_replace("http://www.test.de/","#", $menu);
amburnside
  • 1,943
  • 5
  • 24
  • 43
0
// 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);
Lawson
  • 624
  • 1
  • 5
  • 19
0
  • Change fixed-name domain to #
  • Trim / from the end
  • Replace / with -

    $domain = "http://www.test.de/"
    str_replace('/','-',trim(str_replace($domain, '#', $menu),'/');
    
zavg
  • 10,351
  • 4
  • 44
  • 67