User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,561 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,668 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting

using statements in user defined functions

Join Date: Mar 2007
Location: georgia
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

Re: using statements in user defined functions

  #7  
Mar 28th, 2008
i am sorry for the triple post, i truly am... but i solved this issue after a day's work of googling and thought i could save people the same trouble by posting my solution here. i guess a moderator could delete by past two posts or something.

any way...
i discovered the use of buffering data before it reaches the browser.

  1. <?php
  2. //function that gets html
  3. function getHTML($container_function){
  4. //johndm.com - gets html that results from
  5. //a function. example usage:
  6. // <?php function foo(){ ?|> bar <?php } ?|>
  7. // echo "<b>".getHTML("foo")."</b>";
  8.  
  9. ob_start();ob_clean(); //start buffer
  10. call_user_func($container_function); //call container function
  11. $data=ob_get_contents(); //get data
  12. ob_end_clean(); //remove output from function
  13. return $data; //return html
  14.  
  15. }
  16.  
  17. //example usage
  18.  
  19. function foo(){ ?>
  20. <b>foo</b>
  21. <?php }
  22. $d=getHTML("foo");
  23.  
  24. //now that the html outputted by the
  25. //function is in a string, you can do anything
  26. //to it you could do to a string.
  27.  
  28. echo "i am a ".$d." bar";
  29. echo "<br />";
  30. echo "i am ".strlen($d)." chars long";
  31. echo "<br />";
  32. echo "i have ".substr_count($d,">")." '>'s in me";
  33. echo "<br />";
  34. echo "my html looks like this: ".htmlentities($d);
  35.  
  36. //not only can it get html that is not inside
  37. //of php tags, it can also let you modify
  38. //echo/printed data from a function also.
  39. ?>
  40.  
Last edited by hunkychop : Mar 28th, 2008 at 8:09 pm.
toast
Reply With Quote  
All times are GMT -4. The time now is 6:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC