I have a code:
$html = '<html>
<body>
<p><a href="[LINK]">[LINK]</a></p>
<p><img src="[IMAGE]" alt="something" /></p>
<p>Language specific string: [ANOTHER TAG]</p>
</body>
</html>';
$search = array('[LINK]', '[IMAGE]');
$replace = array($post['link'], $post['image']);
$html = str_replace($search, $replace, $html);
Above will replace [LINK] and [IMAGE] with corresponding $post values – this is good when both values are not empty. I need a way to hide the corresponding paragraph when one or more $post values are empty. Anyone has an idea what would be the most efficient solution?
$html content is loaded using file_get_contents (the above is just an ilustration of its content).
EDIT: Since in the $html I also have “localized” strings, I’d rather use the solution that doesn’t force me to replace the whole paragraph. There was a good answer that used DOM manipulation but the author has deleted it…
Now
[LINK]and[IMAGE]are replace with the whole tag if they are present, and an empty string if not.Depending on how you generate the
$postarray, you may need to use some condition other thanempty()to determine whether they are present, but you get the idea.EDIT
Since these appear to be email template files, I would suggest using the templating functionality of PHP and put some PHP code inside the templates.
Template file
PHP script