Brandito Posted February 7, 2007 Share Posted February 7, 2007 Hey there, I am trying to replace some of my URLs using str_replace and preg_match. But to no avail so far... I have rewritten some URLs with mod_rewrite and now I need to replace those URLs with the rewritten ones. Here is the code I am using for mod_rewrite: Options +FollowSymLinks RewriteEngine on RewriteRule view-tutorial-id-(.*)\.html$ view_tutorial.php?id=$1 I need to setup a script that I can add to my site, I guess thru a PHP require_once? To replace the dynamic version with URLs like view-tutorial-id-"number".html. But I can't seem to figureout how to do this. Can someone please help me out? Thanks, Brandon Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 <pre> <?php $string = <<<STR Try these links: <a href="view_tutorial.php?id=1">link 1</a> <a href=view_tutorial.php?id=2>link 2</a> <a href='view_tutorial.php?id=3'>link 3</a> STR; echo htmlspecialchars($string = preg_replace('/(?<=[="\'])view_tutorial\.php\?id=(\d+)/', 'view-tutorial-id-\1.html', $string)); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Brandito Posted February 7, 2007 Author Share Posted February 7, 2007 Hey Effigy, thanks for the response + code. But I am a little confused on how I should implement this code... Could you please elaborate more on that? Thanks, Brandon Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 The code I posted can be used to modify the source, but after re-reading your post I see that you want something dynamic. Basically, you want to transform the current URL's format before outputting it into links on the page, correct? Quote Link to comment Share on other sites More sharing options...
Brandito Posted February 7, 2007 Author Share Posted February 7, 2007 Exactly . I was hoping it could be done dynamically like you were saying, to where it just transforms the URL before outputting it into the page. I was also hoping I could have the code in a separate PHP file, and call it 'regex.php' or something, and just have it called upon with a require_once or something. So I can continue to add rewrites to as needed without having to tweek a bunch of pages everytime I need to add a new rule. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 You need to mimic what you're doing in mod_rewrite with a function: <pre> <?php $url = 'view-tutorial-id-2.html'; function mod_rewriteify ($url) { return preg_replace('/^view-tutorial-id-(\d+)\.html\z/', 'view_tutorial.php?id=\1', $url); } echo $url, ' => ', mod_rewriteify($url); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Brandito Posted February 7, 2007 Author Share Posted February 7, 2007 Hey Effigy, thanks again for the code. But I am still kinda confused on how to integrate it. I have been trying differen't ways. But I can't seem to get it right. How do I integrate this code? And if I want to rewrite another URL do I just a more lines like this? <pre> <?php $url = 'view-tutorial-id-1.html'; $url = 'view-tutorial-id-2.html'; $url = 'view-tutorial-id-3.html'; function mod_rewriteify ($url) { return preg_replace('/^view-tutorial-id-(\d+)\.html\z/', 'view_tutorial.php?id=\1', $url); } echo $url, ' => ', mod_rewriteify($url); ?> </pre> Thanks, Brandon Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 Whenever you output an URL, you have to pass it through this function in order to get your rewriting. If this is confusing, can you post some of your code that you want this to take affect on? Quote Link to comment Share on other sites More sharing options...
Brandito Posted February 7, 2007 Author Share Posted February 7, 2007 I am doing it with if statements... For a quick example I wrote this: <?PHP if($_GET[id] == '1') { $page .= '<div class="content">Content here</div>'; } elseif($_GET[id] == '2') { $page .= '<div class="content">Content here</div>'; } else($_GET[id] == '3') { $page .= '<div class="content">Content here</div>'; } require_once('template.php') ?> I have a template file setup for all pages on the tutorials. And then I just have it echo the content of each tutorial when they are accessed. Thanks, Brandon Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 What is in the template; or, rather, where are these URLs that you want to change? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.