Using php to replace a text or div content

Please house,

I want to replace a text or bunch of text anywhere they occurred on the site.

Str_ireplace('this content', ' with new things', ' anywhere is seen on my site or pages');

I have tried using gettext() and str_get_html() but am not getting it to work.

I think i need a guide on how to manipulate html Dom using php.

Something like a translator function but can be used not for echoed texts but also for html content.

A simple usage cause is disable or replace submit button for a field whose id =“takeaway”

Any guide or pointer is highly appreciated.


I can simply tell you that in PHP you can change text strings if they have been saved in a variable. But if you want to manipulate the DOM you have to use JavaScript.

Starting from my suggestion, on the Internet you will find many guide.

Yes, really need more information on the circumstances. Do you want to be able to do on-the-fly changes to your site from an admin point of view, or are you hoping to be able to make these changes at run-time? If the latter, then as above you need to be looking at Javascript because, once the page is rendered, your PHP has finished executing.

Thanks, but is website translation or language translation mechanism done by java?
I know i can change div content using java but there are php ways that changes every word in php before is being echoed

Thanks, but java is not a secure approach to what am looking for.

E.g i want a conditional php statement like this if age = 17 then hide the subit button

This is what i intend archiving, is very easy to do if i can wrap the submit button in a variable.
But sad enough those content is dynamical coded to output different form ids and is not such i can edit them manually.
So am looking at a kind of filter that will add or wrap my own function around them

There is what is called gettext() and ngettext() in wordpress which i use most times to translate woocommerce submit button texts and some other texts like change the word posts to feed, Archives to Library
But for some reasons it does not work for code replacement where i want to use such approach to rewrite <div id=1 into not available

Study the below code, i picked it from gravity forms documentation, they used domdocument functions which i suspect it could be a java language but if is not java, then i will like to understand how to use them solve me problems.

`add_filter( ` `'gform_next_button'` `, ` `'input_to_button'` `, 10, 2 );`

`add_filter( ` `'gform_previous_button'` `, ` `'input_to_button'` `, 10, 2 );`

`add_filter( ` `'gform_submit_button'` `, ` `'input_to_button'` `, 10, 2 );`

`function` `input_to_button( ` `$button` `, ` `$form` `) {`

`    ` `$dom` `= ` `new` `DOMDocument();`

`    ` `$dom` `->loadHTML( ` `'<?xml encoding="utf-8" ?>'` `. ` `$button` `);`

`    ` `$input` `= ` `$dom` `->getElementsByTagName( ` `'input'` `)->item(0);`

`    ` `$new_button` `= ` `$dom` `->createElement( ` `'button'` `);`

`    ` `$new_button` `->appendChild( ` `$dom` `->createTextNode( ` `$input` `->getAttribute( ` `'value'` `) ) );`

`    ` `$input` `->removeAttribute( ` `'value'` `);`

`    ` `foreach` `( ` `$input` `->attributes ` `as` `$attribute` `) {`

`        ` `$new_button` `->setAttribute( ` `$attribute` `->name, ` `$attribute` `->value );`

`    ` `}`

`    ` `$input` `->parentNode->replaceChild( ` `$new_button` `, ` `$input` `);`


`    ` `return` `$dom` `->saveHtml( ` `$new_button` `);`

1 Like

Is this really a php language? If it is then am interested in learning how to use it, but if it is a java or client side language then no need pressing

It does appear to be PHP, but it’s enclosed in back-ticks for some reason.

1 Like

I heard is an Api library which most language like php, Java and others have adopted, something similar to bootstrap.

Do any one know how to make use of it and if it can help in my Quest?

If all you want to do is conditionally replace text and considering your example of a submit button then why not either
a) read the file contents into a string and do a search / replace before it is executed
b) use an if… else to echo the desired html code conditionally
c) place the various code elements in separate files and use include() to place the desired html code

1 Like

Thanks, but option b and c is out of the picture because i dont have the source code to modify them, they came with externally.

So how do i use option A?
Let me see sample work around

Have a read up on file_get_contents(), play with it and see what you come up with.

I am confused now, If you do not have the source code, how do you know what you want to replace and how are you ever going to replace it

If you have access to a file then the process would be something like

$content = file_get_contents("filename");
$newcontent = str_replace("text to replace", "replace with this", "$content");
file_put_contents("filename", "$newcontent");

Basically read the file contents into a string $content, create a new string with the items replaced, $newcontent and save it. But you have then lost the original and replaced it with your new version. So you need at some point presumably to set it back again. I think you need to provide more info. But the example above will modify a file contents.

I can see the content in html source when viewed in browser. Then my own plugin if activated will replace the content with my choice

I know of file get content, you just helped me realize what i should try.

Thanks alot.

OK then you should be able to use the example I provided, however if you are loading the results of a web page automatically into a file you are basically web ‘scraping’. This is a technique used by some to extract emails for spamming or other data.

Some web developers will try and protect against this so you may have to experiment and research before you get a good result

I am still unsure how you will access a completed html page, replace elements in it and then republish it to a user - it seems very convoluted.

Can be done using jquery very easy.
But am not working on a completed html page, is the sorce code that will be altered before it get goes out

I will use str_ireplace() or preg_match ()

Then I guess we are back to PHP, plus JS is client side which I try to avoid where possible, whereas PHP is server side