0

I'm not exactly sure what I need to search for in Google and have been struggling with this for a while.

I wrote my own CMS for a project and am stuck with processing content stored in the database, for example.

This is a link to a <a href="<?php get_url_by_id(487174) ?>">related page</a>.

In the above example I'm getting a page url by its ID, this way it doesn't matter if the url changes, it will always be up to date.

The issue is PHP sees it as a string and will not process it.

Im working around the issue by writing the contents to a file, using PHP include on the file, and then deleting the file. I don't see this as an efficient and would like a better solution.

4
  • Sees and processes what? Commented May 14, 2016 at 12:15
  • Where's the code of get_url_by_id? Commented May 14, 2016 at 12:21
  • <?php echo get_url_by_id(487174) ?> The code is not relevant to my question; It simply inserts the URL. Commented May 14, 2016 at 12:33
  • How are you getting the code from the database to be interpreted as PHP? However you decide to do it, some controlling of what PHP code is allowed to run from the the database, will be required? Commented May 14, 2016 at 14:32

1 Answer 1

1

PHP reads that content as a string because it is a string.

To make your string function as PHP, you'll need to use PHP's eval() function.

// The string that is loaded from the DB, or wherever
$string = 'This is a link to a <a href="<?php get_url_by_id(487174) ?>">related page</a>.'

// Run the string as PHP code (notice the "echo" command)
eval("echo {$string}");

This can be very dangerous, however! If you're going to do this, be very certain you know what string is being executed! Because the eval() function will run any PHP code that is placed in it! Even site-destroying-dog-kicking PHP code!

More about the eval() function can be found in the PHP Docs for eval()

--

I don't know your exact scenario, but I would generally advise against using eval() wherever possible. There is normally a safer way to doing something than using the eval() function.

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

6 Comments

@PedroLobito You're referring to the PHP function that is being called within the string that is being pulled from the DB--which isn't being called at all because PHP interprets it as a string.
I'm referring to function get_url_by_id
@PedroLobito Yes, but that's irrelevant to the OP's question as well as my answer. 'This is a link to a <a href="<?php get_url_by_id(487174) ?>">related page</a>.' is a string.
@PedroLobito why would that function be relevant? Do you even use PHP?
@PedroLobito - congratulations on making an immense contribution to solving someone's problem, don't forget to pat yourself on the back :)
|

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.