Boardunity & Video Forum

Boardunity & Video Forum (https://boardunity.de/)
-   Programmierung und Datenbanken (https://boardunity.de/programmierung-datenbanken-f23.html)
-   -   RegEx - Suchmaschinenfreundliche Links? (https://boardunity.de/regex-suchmaschinenfreundliche-links-t3191.html)

Eduard Zintz 08.06.2005 23:11

RegEx - Suchmaschinenfreundliche Links?
 
Hallo,

ein kleines Problem mit dem gestalten suchmaschinenfreundlicher Links.

PHP-Code:

<?php
error_reporting
(E_ALL);
echo 
'<pre>';
$basehref = 'http://127.0.0.1/';

## Funktioniert
#$hreflink = '<a href="' . $basehref . 'index.php?module=FOO&sid=md51864xg39123">Linkname</a>';

## Funktioniert
#$hreflink = '<a href="' . $basehref . 'index.php?module=FOO&action=BAR&sid=md51864xg39123">Linkname</a>';

## Funktioniert nicht
#$hreflink = '<a href="' . $basehref . 'index.php?module=FOO&action=BAR&newsid=89&sid=md51864xg39123">Linkname</a>';
## Funktioniert nicht
#$hreflink = '<a href="' . $basehref . 'index.php?module=FOO&action=BAR&contentid=57&sid=md51864xg39123">Linkname</a>';

$endlink = preg_replace('#index.php\?module=([\w].*?)&(?(?=action=)action=([\w].*?)&)(?(?=id=).*?id=([\d].*?)&)sid=([\w].*?)"#i', '$1/$2,$3.html?$4', $hreflink);

echo 
htmlspecialchars($endlink);
echo 
'</pre>';
?>


Das Problem ist, es wird ein bisschen anders replaced als gedacht. Das ganze hat ein Problem mit *id im Link.

Ich bin für jede Hilfe dankbar!

Eduard Zintz 08.06.2005 23:57

Problem gelöst, dank der Hilfe von exe im IRC.

Lösung:
PHP-Code:

$endlink = preg_replace('/index\.php\?module=(\w+)(?:&action=(\w+?))?(?:&[\w]+id=(\d+))?&sid=(\w+)/ie', 'makelink("\1", "\2", "\3", "\4")', $hreflink);

function 
makelink($module, $action = '', $id = '', $sessionid = '')
{
    
$sid = $sessionid;
    if (!
$action) $link         = $module . '.html?' . $sid;
    if (
$action && !$id) $link     = $module . '/' . $action . '.html?'. $sid;
    if (
$action && $id) $link     = $module . '/' . $action . ',' . $id . '.html?'. $sid;
    
    return 
$link;
} 

Was falsch war, postet exe *so gehört hab* ;)


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:57 Uhr.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25