preg_replace links from /index.php?section=xxx to section.html

Discussion in 'PHP' started by ptalent, Jul 27, 2009.

  1. #1
    I am writing a SEF mod for standard PHP websites, and have all the htaccess stuff done which takes links like domain.com/dynamic-page.html and reads it as domain.com/index.php?section=dynamic-page

    But the problem is on page links which are either hardcoded or dynamically generated, either way there would be thousands of links to change from index.php?section=xxx to the SEF URL.

    I have got as far as converting the links to <a href="dynamic-page">.... but can get the .html on the end.

    Here's my code:

    ob_start();
    
    .....
    
    $buffer = ob_get_contents(); 
    ob_end_clean(); 
    
    $pattern = '/index\.php\?section=/';
    $replacement = '';
    $buffer = preg_replace($pattern, $replacement, $buffer);
    
    echo $buffer; 
    PHP:
    How do I trap that final bit of the link and stick a .html on the end?
     
    ptalent, Jul 27, 2009 IP
  2. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Do you really have to process the whole page at once? I mean instead of generating a whole page with index..php?section=xxx, then change link to dynamic-page.html, why don't you generate dynamic-page.html at the beginning?

    I'm not sure how do make index.php?section=xxxx on php page, but if you make it by echo, it would be much easy to change it to xxxx.html at this point (echo $xxx.html instead).

    However, if you insist changing link to dynamic.html after the page has been generated, you can try my function (but this approach will be much slower than what I suggest above).

    Assuming you have <a> tag exactly like this format <a href="index.php?section=abc">
    
    
    function relink($buffer) {
    	$store= $buffer;
    	while (strpos($buffer, 'index.php?section=') !== false) {
    		$ind= strpos($buffer, 'index.php?section=') +18;
    		$buffer= substr($buffer, $ind);
    		$lst= strpos($buffer, '"');
    		$dynamic[]= substr($buffer, 0, $lst);
    	}
    	foreach ($dynamic as $lnk) {
    		$store= str_replace("index.php?section=$lnk", "$lnk.html", $store);
    	}
    	return $store;
    }
    
    PHP:
     
    zandigo, Jul 27, 2009 IP
  3. ptalent

    ptalent Peon

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you very much Zandigo! Your solution has worked perfectly for me :)

    I see now that the route I chose made it very difficult for myself. I knew I had to do find index.php?section= then get the wildcard between the = and the closing ".

    The problem wasn't so much of the dynamically generated pages, it was the hard coded links in the many hundreds of pages.

    Thanks again Zandigo :)
     
    ptalent, Jul 28, 2009 IP
  4. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    You are welcomed. I'm glad that i'm able to help you.
     
    zandigo, Jul 28, 2009 IP