Jump to content

Regex debacle


esoltas

Recommended Posts

Now, I've only been learning PHP for half a year, but this stuff totally messes me up. Before I go headlong into explaining what I have done so far and what I'm trying to accomplish, let me preface: this is my first post on PHP Freaks. Forgive me if my question has already been explained (when I answer HTML and CSS questions, I get the same a lot too...) but my grip on regex is weak, and I believe my question is unique and specific. Not to mention that if I receive a vague answer, my knowledge of regex may not be strong enough to apply it to my situation.

 

What I am trying to make is a component of a book review site. This component is a) a form to plug in what the user wishes to change on the book page (the user will be able alter the description, add info, similar to a small-scale wiki)-this part is already complete and b) a php code page which will plug the post variables into an html string, test if the string matches any spot in the original page, and swap in the replacements. This section is partially done, and where I am struggling. What I have accomplished so far unquestionably is formatting the data the user wants to use as a replacement. What I believe I have accomplished--but the pattern for the preg_replace function may be prepared incorrectly--is to prepare a pattern using one of the post variables to locate the content the user wishes to change. What I tried to accomplish but cannot is then writing these changes into the file.

 

With the following, I will try to be as comprehensive and detailed as possible in explanations.

This is /modify.php- it is the form. There are two "groups" of fields: those in which the user identifies the content needing to be changed and those in which the user enters what shall be used as a replacement. The fields with the names "one," "two," "three," and so on are the identifiers. The fields with the names "twoone," "twotwo," "twothree" and so on are the replacements. For this code, assume that $_POST['page'] is the valid url address we wish to alter in later pages. This page is simply passing on a variable from a previous form.

modify.php:

<?
session_start();
if(!session_is_registered($_SESSION['myusername'])){
header("location:http://www.perlib.wsnw.net/index.php");
}

?>

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'>
  <head profile='http://gmpg.org/xfn/11'>
<title>Perlib</title>
  <style type='text/css' media='all'>@import 'http://perlib.wsnw.net/style.css';</style>
<head>
<meta http-equiv='Content-Language' content='en-gb'>
<meta http-equiv='Content-Type' content='text/html; charset=windows-1252'>
<title>Websites Network - Your domain and web hosting is ready!</title>
<meta name='description' content='Free website hosting services for beginners and all webmasters!'>
</head>
<body>
<div id='container'
<div id='banner'></div>
<div id='reasons'>
<ul id='reasons'>
<li>Review books you've read</li>
<li>Add to your reading list</li>
<li>Suggest this to friend(s)</li>
<li>Find related books</li>
</ul>
<div id="sole_column">
<form method="post" action="modifysupp.php">
What sections do you want to change?
1.<input type="text" name="one"><br />
2.<input type="text" name="two"><br />
3.<input type="text" name="three"><br />
4.<input type="text" name="four"><br />
5.<input type="text" name="five"><br />
What do you want to right in those sections in place of the current text?
1.<input type="text" name="twoone"><br />
2.<input type="text" name="twotwo"><br />
3.<input type="text" name="twothree"><br />
4.<input type="text" name="twofour"><br />
5.<input type="text" name="twofive"><br />
<input type="hidden" name="page" value="<? echo $_POST['page']?>" />
<input type="submit" name="submit" value="submit" />

</form>

</div>
</body>
</html>

 

The next code is for modifysupp.php- standing for modify supplement, this is the code page I discussed earlier. This is the backend of the component. It is responsible for manipulating the post variables received in modify.php and adding them to variables to be surrounded by html text as a string. These post variables will either be used as the pattern or the replacements. These are identified by obvious variable names, so  I assume (feel free to ask questions about the code) much detail may not be necessary here. Then I have tried to manipulate the pattern and replacement arrays into a preg_replace function, where the $input (the source of the page I wish to modify) is checked against the pattern. I then use the fwrite (along with open and close) to write the $input (which is now the modified source). There is a probable chance the reason my component isn't working is because of the regex on this page, with which, as I said, I am weak. This is the reason I posted it here.

modifysupp.php:

<?
$patterns = array();
$replace = array();
$url = $_POST['page'];
$input = @file_get_contents($url);
$patterns[0]="/\<tr\>\<td class=\"labels\"\>".$_POST['one']."\<\/td\>\<td class=\"values\"\>*<\/td\>\<\/tr\>/";
$replace[0]="<tr><td class=\"labels\">".$_POST['one']."</td><td class=\"values\">".$_POST['twoone']."</td></tr>";
$input =preg_replace($patterns[0], $replace[0], $input);
if(isset($_POST['two'])){
$patterns[1]="/\<tr\>\<td class=\"labels\"\>".$_POST['two']."\<\/td\>\<td class=\"values\"\>*<\/td\>\<\/tr\>/";
$replace[1]="<tr><td class=\"labels\">".$_POST['two']."</td><td class=\"values\">".$_POST['twotwo']."</td></tr>";
$input =preg_replace($patterns[0], $replace[1], $input);
if(isset($_POST['three'])){
$patterns[2]="/\<tr\>\<td class=\"labels\"\>".$_POST['three']."\<\/td\>\<td class=\"values\"\>*<\/td\>\<\/tr\>/";
$replace[2]="<tr><td class=\"labels\">".$_POST['three']."</td><td class=\"values\">".$_POST['twothree']."</td></tr>";
$patterns[2] = preg_quote($patterns[2]);
$input =preg_replace($patterns[0], $replace[2], $input);
if(isset($_POST['four'])){
$patterns[3]="/\<tr\>\<td class=\"labels\"\>".$_POST['four']."\<\/td\>\<td class=\"values\"\>*<\/td\>\<\/tr\>/";
$replace[3]="<tr><td class=\"labels\">".$_POST['twofour']."</td><td class=\"values\">".$_POST['twofour']."</td></tr>";
$input =preg_replace($patterns[3], $replace[3], $input);
if(isset($_POST['five'])){
$patterns[4]="/\<tr\>\<td class=\"labels\"\>".$_POST['five']."\<\/td\>\<td class=\"values\"\>*<\/td\>\<\/tr\>/";
$replace[4]="<tr><td class=\"labels\">".$_POST['twofive']."</td><td class=\"values\">".$_POST['twofive']."</td></tr>";
$input =preg_replace($patterns[4], $replace[4], $input);
}
}
}
}
print_r($patterns);
print_r($replace);
print_r($input);

fopen($url, "w");
fwrite($url, $input);
fclose($url);
?>


 

Okay. I hope that's everything I can provide. I don't have any errors with the code in the sense that I would receive a parse error. The component is dying quietly somewhere in the code. I can't figure it out. I don't know if I messed up with regex so the preg function is legitimately not finding any matches with the pattern, or that I am not writing the replacements in correctly, or whatever. In other words, I'm lost and in over my head: help. Sorry this post was long. I wanted to be very detailed so my problem could be easy to understand and tackle with the littlest amount of time wasted in explanations. Chatting over a forum is slow telephone tag.

 

Yours,

Esoltas

 

 

 

Link to comment
Share on other sites

After a few grueling hours, I figured it out. I did some homework on regex, and gradually built statements with a simultaneous tester. ;) Yet I can't stop but wonder, my post got lots of views, but no one responded. Is there a reason why? I'd really like to know because I intend on posting (both asking q's and helping out) here more.  :-\

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.