windowsdan Posted October 20, 2013 Share Posted October 20, 2013 (edited) Hi there, Trying to change content in an HTML link before it is output using readfile? Example: <?php //Get link $link = $_POST['link']; //Posted data may contain spaces that need to be + symbol -- then added to end of link $link = strtr($link, array( ''=> '+', )); HERE I NEED TO CHANGE THE OUTPUT SO THE HTML REPLACES CERTAIN ELEMENTS WITH SOMETHING ELSE //Output the HTML page that is the result readfile('http://www.domain.com/result?='.$link.""); ?> Any help would be amazing? Edited October 20, 2013 by windowsdan Quote Link to comment https://forums.phpfreaks.com/topic/283124-change-content-in-readfile-before-output/ Share on other sites More sharing options...
Ch0cu3r Posted October 20, 2013 Share Posted October 20, 2013 (edited) use urlencode when forming the link Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. Use urldecode when retrieving the link Edited October 20, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283124-change-content-in-readfile-before-output/#findComment-1454654 Share on other sites More sharing options...
kicken Posted October 20, 2013 Share Posted October 20, 2013 Don't use readfile if you want to manipulate the contents before outputting it. Use file_get_contents to download the page into a variable, do whatever changes you want to do, then just echo the variable out. Quote Link to comment https://forums.phpfreaks.com/topic/283124-change-content-in-readfile-before-output/#findComment-1454661 Share on other sites More sharing options...
windowsdan Posted October 20, 2013 Author Share Posted October 20, 2013 (edited) Ok cool. The file_get_contents works for what I want but the next bit is quite a bit harder ... I then need to make 'href=' links within the file contents make a post of a variable to another page. But there is more than one 'href' links in the file contents. So Example: <?php //get post data $link = $_POST['link']; //change spaces for + symbols $link = strtr($link, array(' ' => '+',)); //pull the contents of the file at URL location $html = file_get_contents('http://domain.com/results?search='.$link.""); //change required contents before output $html = strtr($html, array('ORIGINAL' => 'REPLACED',)); //output contents echo $html; ?> But the ORIGINAL and REPLACED bit I need to replace all the 'href' links so that they post a string to another PHP page, for grabbing with POST later, when clicked on. Instead of just hot linking to the original link. Hope this make sense Edited October 20, 2013 by windowsdan Quote Link to comment https://forums.phpfreaks.com/topic/283124-change-content-in-readfile-before-output/#findComment-1454685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.