erme Posted June 11, 2012 Share Posted June 11, 2012 Hi, I am including a file with PHP. The file contains basic html. What I want is to be able replace a tag contained in this file with another one when outputted on the page I am including it on. Eg: replace <h3>Title</h3> as in the file with <h1>Title</h1> on the page. I have found this: $str = str_replace(array('<h3>', '</h3>'), array('<h1>', '</h1>', $str); But don't know how I would integrate it with my include: <?php include('file.txt'); ?> Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/ Share on other sites More sharing options...
trq Posted June 11, 2012 Share Posted June 11, 2012 You would need to replace: <?php include('file.txt'); ?> With: <?php ob_start() include('file.txt'); echo str_replace(array('<h3>', '</h3>'), array('<h1>', '</h1>', ob_get_clean()); ?> This puts the contents of the call to include within a buffer that you can then manipulate using whatever functions you like. Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352886 Share on other sites More sharing options...
erme Posted June 11, 2012 Author Share Posted June 11, 2012 Thanks for your reply. I am now getting this error unexpected T_INCLUDE Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352889 Share on other sites More sharing options...
trq Posted June 11, 2012 Share Posted June 11, 2012 Missing ; on ob_start() Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352891 Share on other sites More sharing options...
erme Posted June 11, 2012 Author Share Posted June 11, 2012 After adding that it now comes up with syntax error, unexpected ';' on this line: echo str_replace(array('<h3>', '</h3>'), array('<h1>', '</h1>', ob_get_clean()); Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352892 Share on other sites More sharing options...
floridaflatlander Posted June 11, 2012 Share Posted June 11, 2012 echo str_replace(array('<h3>', '</h3>'), array('<h1>', '</h1>', ob_get_clean()); Are you missing a closing ) on the end? Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352893 Share on other sites More sharing options...
trq Posted June 11, 2012 Share Posted June 11, 2012 These are simple syntax errors, you need to learn to fix them yourself. echo str_replace(array('<h3>', '</h3>'), array('<h1>', '</h1>'), ob_get_clean()); Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352894 Share on other sites More sharing options...
floridaflatlander Posted June 11, 2012 Share Posted June 11, 2012 syntax error, unexpected ';' Usually means you need an extra closing parentheses or bracket or you have an extra parenthesis or bracket. Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352901 Share on other sites More sharing options...
erme Posted June 11, 2012 Author Share Posted June 11, 2012 Thanks guys, Unfortunately nothing is happening now. Just a blank screen. I am getting a warning though: Warning: str_replace() expects at least 3 parameters, 2 given Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352909 Share on other sites More sharing options...
floridaflatlander Posted June 11, 2012 Share Posted June 11, 2012 is this another subject? anyway three arguments str_replace("Replace this', 'With This', $in_this); Did this come about after the above? Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352917 Share on other sites More sharing options...
erme Posted June 11, 2012 Author Share Posted June 11, 2012 I just don't seem to be achieving anything $thecontent = file_get_contents('textfile.txt'); echo str_replace('<h3>', '<h1>', $thecontent); Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352924 Share on other sites More sharing options...
erme Posted June 11, 2012 Author Share Posted June 11, 2012 Tell a lie .. it works perfect. Thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352926 Share on other sites More sharing options...
floridaflatlander Posted June 11, 2012 Share Posted June 11, 2012 Good Quote Link to comment https://forums.phpfreaks.com/topic/263985-replace-html-tag/#findComment-1352929 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.