Jump to content

Preg_match problem...


Brandito

Recommended Posts

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

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.