Reputation: 33
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.
Upvotes: 0
Views: 53
Reputation: 8900
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.
Upvotes: 1