0

I have HTML content in the variable $detail and I want to "embed" PHP files there.

Note: The variable "$detail" gets stores the information from a query to the database.

This variable $detail has paragraphs "<p> </p>" in those paragraphs I need to be able to "embed" PHP files, for example, in the second paragraph, embed "file1.php" the other file in the fifth paragraph.

It is important that it is in PHP

I had already done something similar but with JAVASCRIPT but when the browser has JAVASCRIPT debugging deactivated everything is out of order, that's why I'm looking for some way to use it with PHP

Example:

include 'file1.php';
include 'file2.php';

//They must be included or embedded in the $detail variable, the first file, in the third paragraph, and the second file, in the seventh paragraph
 echo $detail;
  • I don't think you've explained your question well enough for anybody to be able to help you. For instance, it seems you have HTML content in `$details` and you want to "embed" PHP files in there somehow. This sounds like some sort of botched together "php template engine". Have a look for that, and see if that's what you need. – KIKO Software Dec 15 '20 at 13:39
  • @KIKOSoftware If it is exactly what I need, sorry if I was not clear in expressing myself. –  Dec 15 '20 at 13:50
  • @KIKOSoftware I had already done something similar but with JAVASCRIPT but when the browser has JAVASCRIPT debugging deactivated everything is out of order, that's why I'm looking for some way to use it with PHP –  Dec 15 '20 at 13:53
  • I see you changed your question. I can understand it somewhat better now. – KIKO Software Dec 15 '20 at 14:35

1 Answers1

1

You can use output buffering to get file1.php and file2.php, additionally with preg_replace() like:

$detail = "
    <p>1</p>
    <p>...</p>
    <p>3</p>
    <p>4</p>
    <p>...</p>
    <p>6</p>";

ob_start();
include 'file1.php';
$file1 = ob_get_clean();

ob_start();
include 'file2.php';
$file2 = ob_get_clean();

$detail = preg_replace('/^(.*<p>.*<p>).*(<\/p>.*<p>.*<p>.*<p>).*(<\/p>.*)$/Us', "\${1}{$file1}\${2}{$file2}\${3}", $detail);

echo $detail;

However you should check your business logic or use a PHP template engine, like Smarty as @kiko-software said earlier.

Stocki
  • 463
  • 4
  • 14
  • This might work, but wouldn't it be a better idea to slightly change `file1.php` and `file2.php` so that a function in them returns a string, instead of manipulating the output buffer? When the code gets more complex this will become a problem. – KIKO Software Dec 15 '20 at 14:36
  • @KIKOSoftware that's a good idea, @Mary, PHP has a good example here: https://www.php.net/manual/en/function.include.php#example-145 Besides, if we can't modify `file1.php` / `file2.php`, because they are, for example, ionCube encoded, then may OB do the trick. – Stocki Dec 15 '20 at 14:46
  • @Stocki Thanks, practically the first file contains information about this may also interest you, and the last file has advertising. –  Dec 15 '20 at 15:01
  • @Stocki I had thought of creating content in parts, but this is not a good idea to have to create three additional tables in the database. First because in the administrator it would have to create three textarea and second it would have to explain its use to the user then therefore discarded. –  Dec 15 '20 at 15:05
  • @KIKOSoftware How about an implementation like this: https://stackoverflow.com/questions/39076511/how-to-split-an-html-string-into-chunks-in-php/39076776 would also have problems in the future or not? –  Dec 15 '20 at 18:43
  • @Mary: I can't be sure what you're referring to on that page, so I cannot answer your question. I think that Stocki gave you quite a bit of help. – KIKO Software Dec 15 '20 at 20:16
  • @KIKOSoftware Well, according to what I understand in that question, it divides a variable into parts or fragments, according to the first comment of this answer, this alternative may have problems later on, that's what I don't want. I only mentioned it in case there is any other improvement, thanks. –  Dec 15 '20 at 20:25
  • @Mary: Oh, I see. No, it's not the string manipulation that I find problematic, it's the use of [the output buffer](https://www.php.net/manual/en/book.outcontrol.php): The `ob_start()` and `ob_get_clean()`. – KIKO Software Dec 15 '20 at 20:41