anon271334
anon271334

Reputation:

Changing Text in PHP

I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...

I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.

So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:

<?php
    $something = "this will replace existing text in the something paragraph...";
?>

Can somebody please point me in the right direction? As the above did not work.

Thank you :)

UPDATE


I was able to get it working using the following sample:

Place this code above the <html> tag:

<?php
$existing  = "default message here";
    $something = "message displayed if form filled out.";
    $ne = $_REQUEST["name"];

    if ($ne == null) {
      $output = $existing;
    } else {
      $output = $something;
    }
?>

And place the following where ever your message is to be displayed:

<?php echo $output ?>

Upvotes: 0

Views: 24713

Answers (4)

Dan
Dan

Reputation: 133

If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.

Upvotes: 0

Ankur Mukherjee
Ankur Mukherjee

Reputation: 3867

If you are looking for string manipulation and conversion you can simply use the str_replace function in php.

Please check this: str_replace()

Upvotes: 0

Thilo
Thilo

Reputation: 171

If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.

If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 158005

As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:

<?php
    $existing  = "existing text";
    $something = "this will replace existing text in the something paragraph...";
    if (empty($_GET['button'])) {
      $output = $existing;
    } else {
      $output = $something;
    }
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>

but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?

Upvotes: 2

Related Questions