Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

putting multiple functions together

Status
Not open for further replies.

manicleek

Technical User
Jun 16, 2004
143
GB
how can I put multiple functions into one parent function,

for example, if on my out put I normaly have:

<?= nl2br(ucfirst(striptags($sometext))) ?>

how can I define all those at the beginning of the page so I would only have to put something like:

<? textformat($sometext) ?>
 
You create a user-defined function that takes as is input the innermost text, runs that text through all the functions, and returns the result.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Here's what I would do:
Code:
<?php
function textformat($str)
{
   return (nl2br(ucfirst(striptags($str))));
}
?>

Ken
 
so:

function textformat($sometext) {

striptags($sometext);
ucfirst($sometext);
nl2br($sometext);

}

is that correct?
 
No.

Take a look at kenrbnsn's example.

What you left out was returning a value.
What you did wrong in your function was to run the output of one function into the next function. You just performed three transforms on the same single text.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
thanks for that, think we were posting at the same time, never saw that example
 
After this is up and going, you might also want to add some "error handling"..(note the quotes)

Code:
<?php
function textformat($str = "404")
{
  if ($str == "404") {
      return "404 - nothing to format"
    }
  else {
      return (nl2br(ucfirst(striptags($str))));
    }
}
?>

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top