Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialROHIT SINGLA
Courses Plus Student 708 PointsUse File_get_contents To Get Contents Of File And Delete Certain Part Of data that doesnot pass filteration
Hii How to Use File_get_contents To Get Contents Of File And Delete Certain Part Of data that doesnot pass filteration?
For eg: fetch a html page and delete the adults words in the html page and then display the rest of the page.
1 Answer
Jose Soto
23,407 PointsHello! You can use file_get_contents to grab all the data from a file, str_replace to delete certain words, then file_put_contents to overwrite the file data. Here is an example:
<?php
/**
* This is an example PHP script to delete all of the
* vowels from a text file.
*/
//create a variable to point to the filename
$file = 'test_file.txt';
//move the contents of the file into a variable
$current = file_get_contents($file);
//create a variable to hold everything you want to search for in the contents
//in your case, this could be an array of adult words
$vowels = array("a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y");
//replace each of the searchable items above with a ""
//this esentially deletes those items in the array
$onlyconsonants = str_replace($vowels, "", $current);
//replace the contents of the file with the result of
//the string replace (str_replace) command above
file_put_contents($file, $onlyconsonants);
?>