Jump to content

[SOLVED] PHP Preg_Replace Problem - Help - Unable To Get It To Work...


lifetalk

Recommended Posts

I'll start with the basics..

 

I am trying to create an input form, that'll accept a username or a text, pass over that data to a PHP script.

 

The PHP script needs to open an existing file on a server (it's an HTML file) and search for a specific text (the part where username is) and replace it with the username that was just posted to the script from the form.

Then, close the file after editing it, create a copy, rename that copy and offer it for download to the end user.

 

I figured this involved fopen, fwrite, fclose, preg_replace, copy, rename, POST to get it done.. but apparently, I am wrong.

 

index.php:

<html>
<form method="POST" action="processme.php">
enter your username: <input type="text" name="username" /><br />
<input type="submit" name="submit" value="Yahoo!" />
</form>

 

processme.php:

<?php

//define stuff

$username = $_POST['username'];
$filename = "generate.html";

$source = $filename;
$copy = $username;

$search = "user=(.*)";
$replace = "user=$username";

//open the file, and read it
$handle = fopen($filename, "r+");

//search for the username, and replace it
$new = preg_replace($search, $replace, $handle);

//write to file
fwrite($handle, $new);
fclose($handle);

//copy the file
copy($source, $copy);

//Rename file
rename($copy, $username);

?>

 

the file that needs to be edited, written to, and then cloned:

<style type="text/css">
#container
{
width: 1024px;
height: 1496px;
overflow: hidden;
border: 0px;
}

#container iframe
{
width: 1024px;
height: 1576px;
margin: -80px 0 0 0;
}

</style>
</head>
<body>
<div align="center">
<img src="http://wwwstatic.megaporn.com/video/gui/logo_mprn.gif" />
</div>
<div id="container">
<iframe src="http://megaporn.com/v/?c=profile_videos&user=lifetalk" scrolling="no" frameborder="0"></iframe>
</div>
</body>
</html>

 

Any ideas where I'm going wrong?

 

Thanks,

lifetalk

 

P.S: I am not a pro. I can call myself a noob, for all I know!

Link to comment
Share on other sites

change this...

 

//search for the username, and replace it
$new = preg_replace($search, $replace, $handle);

//write to file
fwrite($handle, $new);
fclose($handle);

 

 

to this...

 

$size = filesize ( $filename );

//search for the username, and replace it
$new = preg_replace($search, $replace, fread ( $handle, $size ) );

ftruncate ( $handle, $size );

rewind ( $handle );

//write to file
fwrite($handle, $new);
fclose($handle);

Link to comment
Share on other sites

What results are you getting?

 

This is the error it returns

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\Program Files\VertrigoServ\www\new\processme.php on line 18

 

 

change this...

 

//search for the username, and replace it
$new = preg_replace($search, $replace, $handle);

//write to file
fwrite($handle, $new);
fclose($handle);

 

 

to this...

 

$size = filesize ( $filename );

//search for the username, and replace it
$new = preg_replace($search, $replace, fread ( $handle, $size ) );

ftruncate ( $handle, $size );

rewind ( $handle );

//write to file
fwrite($handle, $new);
fclose($handle);

 

Thanks. Tried that code. The error now is

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\Program Files\VertrigoServ\www\new\processme.php on line 20

 

Processme.php is now:

<?php

//define stuff

$username = $_POST['username'];
$filename = "generate.html";

$source = $filename;
$copy = $username;

$search = "user=(.*)";
$replace = "user=$username";

//open the file, and read it
$handle = fopen($filename, "r+");

$size = filesize($filename);

//search for the username, and replace it
$new = preg_replace($search, $replace, fread($handle, $size));

ftruncate($handle, $size);

rewind($handle);

//write to file
fwrite($handle, $new);
fclose($handle);

//copy the file
copy($source, $copy);

//Rename file
rename($copy, $username);

?>

 

Any ideas? I'm totally stumped!

 

 

Link to comment
Share on other sites

Works... No errors.. However, it sort of truncates quite a bit of essential code:

 

Original Code (html):

<html>
<head>
<style type="text/css">
#container
{
width: 1024px;
height: 1496px;
overflow: hidden;
border: 0px;
}

#container iframe
{
width: 1024px;
height: 1576px;
margin: -80px 0 0 0;
}

</style>
</head>
<body>
<div align="center">
<img src="http://wwwstatic.megaporn.com/video/gui/logo_mprn.gif" />
</div>
<div id="container">
<iframe src="http://megaporn.com/v/?c=profile_videos&user=check" scrolling="no" frameborder="0"></iframe>
</div>
</body>
</html>

 

New code after running script:

Resource id #3
<style type="text/css">
#container
{
width: 1024px;
height: 1496px;
overflow: hidden;
border: 0px;
}

#container iframe
{
width: 1024px;
height: 1576px;
margin: -80px 0 0 0;
}

</style>
</head>
<body>
<div align="center">
<img src="http://wwwstatic.megaporn.com/video/gui/logo_mprn.gif" />
</div>
<div id="container">
<iframe src="http://megaporn.com/v/?c=profile_videos&user=eh
</div>
</body>
</html>
er="0"></iframe>
</div>
</body>
</html>

 

Processme.php Code:

<?php

//define stuff

$username = $_POST['username'];
$filename = "generate.html";

$source = $filename;
$copy = $username;

$search = "/user=(.*)/i";
$replace = "user=$username";

//open the file, and read it
$handle = fopen($filename, "r+");
$size = filesize($filename);

//search for the username, and replace it
$new = preg_replace($search, $replace, fread($handle, $size));
ftruncate ( $handle, $size );
rewind ( $handle );

//write to file
fwrite($handle, $new);
fclose($handle);

//copy the file
copy($source, $copy);

//Rename file
rename($copy, $username . ".html");

?>

 

Thanks for all the help so far :)

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.