views:

57

answers:

4

Hey all

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 ?>
+1  A: 

if you are looking for string manipulation and conversion you can simply use str_replace function of php.

please check this: str_replace()

Ankur Mukherjee
+1  A: 

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?

Col. Shrapnel
Hi. Thanks for your answer. The code in your answer didn't work, it just kept displaying the 'existing' message. However, after editing/removing parts of it, I got it to work. I've updated my question to include what worked. Thanks :)
lucifer
@j-t-s, Very ugly. You should be ashamed.
Chase
What's so ugly about it? I'm only new to PHP. And btw, my version is not very different from @Col's at all. I assumed it that it would be acceptable.
lucifer
+1  A: 

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.

Dan
+2  A: 

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.

Meloth