I'm new to php and have the following code to show the string there's inside a .txt file in my web:
<?php
$file = "file.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;
}
?>
I'd like to know how to select some words to be hidden. In my case I want to hide the numbers (01,02,03,04,05,1,2,3,4,5) in the line. I also want to replace the whole line for another one in case it starts with a certain word. For example if the line starts with the word "example" replace the whole line and only display the words "hello world"
To remove the numbers:
$str = preg_replace("/\d/", "", "This 1 is 01 2 a 2 test 4 45 aaa");
echo $str;
Output:
This is a test aaa
Link to fiddler
To replace the the whole line (only if it starts with "example") with "hello world":
$str = "example This 1 is 01 2 a 2 test 4 45 aaa";
echo preg_replace("/^example.*/", "hello world", $str);
Output:
hello world
Link to fiddler
Combining both together will give us:
$file = "file.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
$line = preg_replace("/^example.*/", "hello world", $line);
$line = preg_replace("/\d/", "", $line);
print $line;
}