0

I have this tag in PHP file, and I need to remove it when user is in Main Page (mainpage.com example).

I don't need to hide it with CSS or JS, just remove from DOM.

I have searched the web, but can't find nothing (maybe it's my mistake) (also I'm a frontend Angularjs developer)

So how can I do it with PHP?

<td class="category" width="20">
     <a>
          ...
     </a>
</td> 
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    You mean that this code is in a file that is include in multiple pages, and you want to exclude some code from that file for a single page? `if ($_SERVER['REQUEST_URI'] != '/') { ... }` should do? – jcaron May 19 '17 at 00:10
  • @jcaron Yes , this code is include in every pages of website , i need to remove it only from main page –  May 19 '17 at 00:12
  • The answer is in my comment then. Add the appropriate `` in the right places, of course. – jcaron May 19 '17 at 00:13
  • @halfer , will done :) –  May 19 '17 at 00:14
  • @jcaron Can you write a right answer below please ? –  May 19 '17 at 00:14

2 Answers2

0

First you need to make an if else conditional for current url. To get current url in PHP you can read this question Get the full URL in PHP

if ($url === 'http://mainpage.com/') {
//function here 
enter code here
} else {
// do nothing 
}

To remove the content of certain element you can use preg_replace function. I tested this code bellow and it's worked.

$html = '<div id="somediv">Some div</div>
    <td class="category" width="20">
    <a> ...  </a> </td>

    <td class="anothercategory" width="20">
    <a>
    Not removed
    </a>
    </td><
    <div class="someclas"> Another Part</div>';

echo  preg_replace('/<td class="category"[^>]*>([\s\S]*?)<\/td[^>]*>/', ' Removed ', $html);

Please combine that two code based on your case. Hope it's help

Community
  • 1
  • 1
rheeantz
  • 960
  • 8
  • 12
0

Normally will control by PHP 'IF' condition, but need depend how your website detect current page is main page.

If you home page URL look like:

  • mainpage.com
  • mainpage.com/index.php

I then the example below should help you up :)

<?php if(!(strtok($_SERVER['REQUEST_URI'], '?') == '/' || strtok($_SERVER['REQUEST_URI'], '?') == '/index.php')):?>
<td class="category" width="20">
     <a>
          ...
     </a>
</td>
<?php endif; ?>
Mlax Wong
  • 130
  • 1
  • 8