943,877 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1200
  • PHP RSS
Mar 28th, 2008
0

using statements in user defined functions

Expand Post »
is there a way to create a function that uses values from the statement inside of brackets.

ex.
php Syntax (Toggle Plain Text)
  1. myFunction(){
  2. //data here
  3. }

if not, what i am trying to do is get a html tag and check/modify without having it inside of a variable or echo tag.

for example, i want to do the equivilent of this:
php Syntax (Toggle Plain Text)
  1. <?php
  2. $data ={
  3. ?>
  4. test
  5. <?php
  6. }
  7. myFunction($test);
  8. ?>

i do not want to use javascript for this although it would be easier.
thanks!
Similar Threads
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
hunkychop is offline Offline
55 posts
since Mar 2007
Mar 28th, 2008
0

Re: using statements in user defined functions

Although the syntax of your example is incorrect, yes, you can create a function to manipulate the data.

eg.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. function myFunction($input) {
  3. $output = strtolower($input);
  4. return $output;
  5. }
  6.  
  7. $data = 'TEST';
  8. $result = myFunction($data);
  9.  
  10. echo $result;
  11. ?>
Last edited by Suomedia; Mar 28th, 2008 at 12:42 pm.
Reputation Points: 15
Solved Threads: 19
Junior Poster
Suomedia is offline Offline
154 posts
since Mar 2008
Mar 28th, 2008
0

Re: using statements in user defined functions

Click to Expand / Collapse  Quote originally posted by hunkychop ...
if not, what i am trying to do is get a html tag and check/modify without having it inside of a variable or echo tag.
the function i am writting needs to take the data contained in a statement>
html Syntax (Toggle Plain Text)
  1. <?php
  2. checkConfig(){
  3. ?>
  4. <input type="text" name="input" />
  5. <?php
  6. }
  7. ?>
then check if the configuration applies to it and then return the altered html.
i dont have issues with the "meat" of the function.
i will be constantly changing the data that i want to run through checkConfig() so putting the data in a variable ex. <?php $data="<input....."; ?> would not be efficient or helpful.

maby you can get the same results with a class?
Last edited by hunkychop; Mar 28th, 2008 at 1:32 pm.
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
hunkychop is offline Offline
55 posts
since Mar 2007
Mar 28th, 2008
0

Re: using statements in user defined functions

Anything that is outside PHP tags is not available to PHP to manipulate.

Its hard to understand exactly what you are trying to do - why do you wish to manipulate the HTML? What is "the configuration". What is "the data contained in a statement"?

Why not just have PHP output the correct HTML based on your "configuration"?


Matti Ressler
Suomedia
Reputation Points: 15
Solved Threads: 19
Junior Poster
Suomedia is offline Offline
154 posts
since Mar 2008
Mar 28th, 2008
0

Re: using statements in user defined functions

i wrote the below script but it is not done

note: what i will eventually write is not as basic as just bolding the text. i just put that there as an example.

example check function
php Syntax (Toggle Plain Text)
  1. <?php
  2. function check($d){
  3. require('config.php'); // contains $boldtext = true;
  4. if($boldtext){
  5. return "<b>".$d."</b>";
  6. }
  7. }?>

example data:

php Syntax (Toggle Plain Text)
  1. <?php class data{function get(){ ?>
html Syntax (Toggle Plain Text)
  1. This is the text that will go through the check() function
php Syntax (Toggle Plain Text)
  1. <?php } function ret($string){echo $string;}}$data = new data(); ?>
  2.  
  3.  
  4. <?php
  5. $data->ret(check($data->get()));
  6. ?>
Last edited by hunkychop; Mar 28th, 2008 at 5:01 pm.
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
hunkychop is offline Offline
55 posts
since Mar 2007
Mar 28th, 2008
0

Re: using statements in user defined functions

i dont need to "technicaly" or "directly" manipulate the html, i just need to grab the html and then echo it modified.

let me rephrase this...

when i run this....
<?php function test(){ ?>
data
<?php } ?>

everytime a run test() in php, the html outside of the tags is outputted (data)

i want to take the data in test() and be able to do anything with it that i could do with a normal string.

i DO NOT want to do this.
echo "html here..";

or

<?php
$data = <<<_
html here...
_;
echo $data;
?>

i want to have the text outside of the php tags, grab it, and put it into string so i can echo it later. I cant quite phrase why i need to have the origional html outside of the php tags but you will just have to trust me that it is what i want. I dont know if i am on the right track with using a function to get the data, but i cant find any other way to get it,

i have tried doing something like this: <?php function test(){ return ?>html here...<?php ; } ?>
but nothing is returned.

Thanks for your help so far.
Last edited by hunkychop; Mar 28th, 2008 at 7:54 pm.
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
hunkychop is offline Offline
55 posts
since Mar 2007
Mar 28th, 2008
0

Re: using statements in user defined functions

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.

php Syntax (Toggle Plain Text)
  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. ?>
Last edited by hunkychop; Mar 28th, 2008 at 9:09 pm.
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
hunkychop is offline Offline
55 posts
since Mar 2007
Mar 28th, 2008
1

Re: using statements in user defined functions

Perhaps something like this?


PHP Syntax (Toggle Plain Text)
  1. <?php
  2. function myfunction($buffer) {
  3. return str_replace('hello', 'goodbye', $buffer);
  4. }
  5. $mystuff = ob_start("myfunction");
  6. ?>
  7. I am here to say hello
  8. <?php
  9. ob_end_flush();
  10. ?>

Matti Ressler
Suomedia
Reputation Points: 15
Solved Threads: 19
Junior Poster
Suomedia is offline Offline
154 posts
since Mar 2008
Mar 28th, 2008
0

Re: using statements in user defined functions

thanks! that does work simpler than my method.
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
hunkychop is offline Offline
55 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: send email Undefined variable:
Next Thread in PHP Forum Timeline: function in php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC