0

Is there a way to search the PHP content body while it is being parsed by PHP for certain keywords, so I can include CSS and JS files based on the presence of those keywords before the page is being served?

If PHP would have a variable like $_CONTENT_BODY where it outputs the parsed page into, then I could search for keywords like in this example:

if(stripos($_CONTENT_BODY, 'usetinymce') !== false)) echo '<script type="text/javascript" src="http://a.host.com/js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>';

4 Answers 4

1

You could use ob_start(); and $text = ob_get_contents();. Just make sure that you process these properly, check out http://php.net/manual/en/book.outcontrol.php.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's what I was looking for!
0

You can get the page itself and parse it.

<?php   
if (!isset($_GET['doNotCallMeAgain']) || $_GET['doNotCallMeAgain'] !== 1) {
    $urltothisfile = 'urltothefilehimself'+'?doNotCallMeAgain=1';
    $result = file_get_contents ($urltothisfile); //in $result you will get your page as html and you can search in it
} ?>

3 Comments

That suggests the page is completely generated and served. That's not what I want.
the page will completely generated and served to the server that asked the page itself, not to the client who asked that. Then you can make your check in here and decide how to serve the page to the original request.
beware, with file_get_content used like that you loose Session, cookies, headers, post & get, …
0

You can assemble a Variable which you can search and output at the End.

$_CONTENT_BODY  = 'here ';
$_CONTENT_BODY .= 'is some ';
$_CONTENT_BODY .= 'content.';

if(stripos($_CONTENT_BODY, 'some')){
 // do whatever you like
}

Comments

0

You can include another php file and get the results with ob_start/ob_get_clean()

ob_start();
include 'myfile.php'; 
$_CONTENT_BODY = ob_get_clean();

But you will encounters some problems, like white pages on some php errors. It is better to build your app more logicaly instead of doing some dirty search & replace.

1 Comment

Now that I've read the manual, in your example it's safer to use $_CONTENT_BODY = ob_get_flush(); which also stops output buffering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.