John Davidson

how to replace specified characters with space in a text file with php

0 comments
Message:


text files name is 32viestit21.txt


<?php
if(isset($_POST['send']))
{
if (isset($_SESSION['username']))
$fp = fopen('32viestit21.txt', 'a',1);
$message=$_POST['message'];

// code here

fwrite($fp, "$_SESSION[username]: $message".PHP_EOL);
echo "<script>if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
document.location.reload()</script>";
}
?>

and the reason why i ask this is that my website has chat and the chat has xss problem and i have to fix it and chat works so that users inputs goes to txt file then php shows the txt file data.



My Answer:
You can use the following PHP code to replace specified characters with spaces in a text file:

php

// Specify the characters you want to replace
$charactersToReplace = array('-', '_', '.');

// Open the text file for reading and writing
$filename = '32viestit21.txt';
$file = fopen($filename, 'r+');

// Read the contents of the file
$content = fread($file, filesize($filename));

// Replace specified characters with spaces
$content = str_replace($charactersToReplace, ' ', $content);

// Move the file pointer to the beginning of the file
rewind($file);

// Write the modified content back to the file
fwrite($file, $content);

// Close the file
fclose($file);

echo "Specified characters replaced with spaces in the text file.";

?>


Make sure to replace the `$charactersToReplace` array with the characters you want to replace in the text file. This code will open the specified text file, read its contents, replace the specified characters with spaces, and then write the modified content back to the file.

Rate this post

3 of 5 based on 1927 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us