Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6885005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:34:49+00:00 2026-05-27T05:34:49+00:00

I have a code: $html = ‘<html> <body> <p><a href=[LINK]>[LINK]</a></p> <p><img src=[IMAGE] alt=something /></p>

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:34:50+00:00Added an answer on May 27, 2026 at 5:34 am
    $html = '<html>
                <body>
                    [LINK]
                    [IMAGE]
                </body>
             </html>';
    $search = array('[LINK]', '[IMAGE]');
    $replace = array(
      (!empty($post['link'])) ? "<p><a href=\"{$post['link']}\">{$post['link']}</a></p>" : '', 
      (!empty($post['image'])) ? "<p><img src=\"{$post['image']}\" alt=\"something\" /></p>" : ''
    );
    $html = str_replace($search, $replace, $html);
    

    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 $post array, you may need to use some condition other than empty() 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

    <html>
      <body>
    <?php if (!empty($post['link'])) { ?>
        <p><a href="<?php echo $post['link']; ?>"><?php echo $post['link']; ?></a></p>
    <?php } if (!empty($post['image'])) { ?>
        <p><img src="<?php echo $post['image']; ?>" alt="something" /></p>
    <?php } if (!empty($post['another_tag'])) { ?>
        <p>Language specific string: <?php echo $post['another_tag']; ?></p>
    <?php } ?>
      </body>
    </html>
    

    PHP script

    ob_start();
    include('template.html');
    $html = ob_get_clean();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: HTML: <img class=d src=i3.jpg alt=/><img class=d src=i4.jpg alt=/> CSS: img.d{margin-top:10px;margin-left:20px;}
I have this code : HTML <a href="javascript:void(0);" id="showDiv">Show Agency</a> <div id="invisibleDiv"> <script src="http://platform.linkedin.com/in.js"
i have html code that looks like the following: String html = <html><head><style type=\text/css\></style></head><body><div
i have html code block like this <div id=some-div> <a href=# class=link-click>some link text</a>
I have this code: HTML <div class=box_meteo> <div class=box_meteo_pulsanti> <a href=javascript:void(0); class=box_meteo_pulsante id=meteoButton1>Link</a> </div>
I have this code in HTML that works fine: <link href=/mywebsite/styles.css rel=stylesheet type=text/css/> However,
I have this code : HTML (meaning 4.0) <div class="temperatura"> <a href="/link/" class="temperatura_localita"> <span
I have this code : HTML <div class="temperatura"> <a href="/link/" class="temperatura_localita"> <div style="padding-left:12px;"> Text1
I have HTML code like this : <div> <a>Link A1</a> <a>Link A2</a> <a>Link A3</a>
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.