Question

So right now, I have a simple function that I use to call some text content:

function htmlstuff() { ?>
<p>html text content here</p>
<? }

And on a page I call the text using:

<?php htmlstuff() ?>

Now, I need to figure out how to use "search and replace" for whatever text is in the function. I've tried things like

function str_replace($search,$replace,htmlstuff())

but I obviously don't know what the heck I'm doing. Is there any simple way to just search the text within the function and search/replace?

Was it helpful?

Solution

what do you really want to do? if you want to search and replace and in the return variable of your htmlstuff function then you are not too far away from the correct answer.

function htmlstuff() { 
 $htmlstuff = "<p>html text content here</p>";
 return $htmlstuff;
}

echo htmlstuff();

str_replace($search,$replace,htmlstuff());

this should do the trick

if you just want to make the function htmlstuff more dynamic, then you should take a different approach. something like this:

function htmlstuff($html) { 
 $htmlstuff = "<p>".$html."</p>";
 return $htmlstuff;
}

echo htmlstuff("html text content here");

OTHER TIPS

It would seem like this:

function htmlstuff ($content = "Default text goes here")
{
  echo "<p>" . $content . "</p>";
}

and then on another call you just htmlstuff("New text to go there");

And if I'm wrong correct me to solve the problem

<?php

$html_stuff = htmlstuff();
$search_for = "Hello, world!";
$replace_with = "Goodbye, world!";

$html_stuff = str_replace($search_for, $replace_with, $html_stuff);

echo $html_stuff;

function htmlstuff() { 
echo '<p>html text content here</p> ';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top