I would like to do a search and replace on the following code:
$this->html('headelement');
Search [tt]<body[/tt], replace with <body something.
How do I do this?
I would like to do a search and replace on the following code:
$this->html('headelement');
Search [tt]<body[/tt], replace with <body something.
How do I do this?
You can use the PHP function str_replace() to do this.
Can you show me explicitly with the example I provided? the examples they provide don’t help
Well, your example was not clear. Do you mean that the data is in: $this->html(‘headelement’);
and you want to put it inside the tag? If so, it would be something like this:
Lets say your web page data is in variable $web_page…
[php]
// Web page data is in $web_page
$new_data = $this->html(‘headelement’);
$web_page = str_replace("<body", "<body " . $new_data, $web_page);
[/php]
Note tested, just from my mind. It replaces the opening <body with <body NEW-DATA which should change the
body tag from to … Try it and let us know if it does not work…
EDITTED: NOTE: if you are using a library, it would be better to use that. Also, if the $this is pointing at body
tag, you would do the replace differently… You did not show us much of your code…
I don’t know how the back-end code works to be honest. All I know is that when I write[tt] $this->html(‘headelement’);[/tt], it spits out stuff. So what I wanted was to inject something inside the without rewriting the back end stuff
LOL, well, you gave a little more info for us to puzzle with…
Well, as a wild guess you are using a library that points you at the header. The $this is an object and the
$this->html is a structure in the object. So, therefore, you can just set it. If the current header is displayed
using this command: echo $this->html(‘headelement’); Then, you can reset that value to whatever you
want it to be using something like this:
$this->html(‘headelement’) = “This is the new heading!”;
BUT, this really depends on the rest of your code. Since you are only showing use one partial line of code, it is
just a guess. But, try it and let us know…
I get this error:
PHP Fatal error: Can’t use method return value in write context in
Here’s what the code looks like
[php]class MobileThemeTemplate extends QuickTemplate {
var $skin;
function execute() {
global $wgRequest;
$this->skin = $skin = $this->data['skin'];
$action = $wgRequest->getText( 'action' );
// Suppress warnings to prevent notices about missing indexes in $this->data
wfSuppressWarnings();
$this->html('headelement');
?>[/php]
Believe me, I’m not trying to hide any of the code, I just don’t understand object oriented PHP :’(
Well, what you are showing is a Wordpress class. Are you using a Wordpress system?
If so, here is a link explaining all about headers… https://codex.wordpress.org/Designing_Headers
Not really sure what you are attempting to do…
Another way you could try doing this, is to write a javascript routine that will replace the string, for php is server side and javascript is client side (meaning it will load last). Though the only downside is people will have to have javascript enable, but I think you’re seeing less and less people disabling javascript. Just some food for thought.
It’s not wordpress related. I just want to add some content into .
How could you do this using javascript?
Well, you showed us template type of code that accesses the header with an OBJECT. You should be able to
store your values into the object. Or, use the str_replace to replace the header. Or, use JS to rewrite the doc’s
header which you can find here a few examples down:
http://www.w3schools.com/js/js_htmldom_html.asp
All of these ways would work for you. Try one or two or three and let us know where it fails…