Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: PHP remove empty <ul></ul> & <li></li>?

  1. #11
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    Hi djr33,

    I can't thank you enough for how much you have helped me regard this issue.

    Even commercially paid advice is nothing comparable to your input in this issue.

    Now I'll need to read and reread (for there are so much information in your two replies) your advice/recommendations above and really try out each method to see which works best.

    I'm very grateful for your time and effort

    Again...Thanks!

  2. #12
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    Hi,

    Not making any progress at all...

    Having came to the conclusion that every Field Value would have a Field Caption Value if there is any value, I decided to display or not display the <li></li> by validating whether the Field Caption Tag has a value or not.

    Tried the followings:

    Code:
    <ul>
    <?php
      $var = '{field1_caption_tag}';
      if ($var != "") 
      echo '<li>{field1_caption_tag}<span>{field1_value_tag}</span></li>';
    ?>
    </ul>
    Problem is, the $var string is still parsed as a Template Tag instead of the Field Caption Value from Database; therefore, it isn't an empty string.

    By the time the output was displayed, only then the Template Tag is displayed as the Field Value from Database.

    Hence, the above method does not work and neither using djr33's worked either; as both methods requires that the HTML elements needs to be validate as empty is order to manipulate the output.

    I have attempted to use Regex and found it to be very very powerful but on second thought, this website uses 4 languages (English, Chinese, Japanese, Thai) and I think it would a major problem using Regex to validate characters of different languages

    Stuck

  3. #13
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    In order to be able to make any further attempt, I have to resolve this problem first.

    So far, I have attempted preg_match, str_replace and ob_start() function; but was unsuccessful due to one same problem, that it was unable to parse the empty HTML tags as an empty element because of the Field Tags in the HTML elements.

    Example:

    Code:
    $val = '{field1_caption_tag}';
    // {field1_caption_tag} is the Field tag which was parsed in the backend 
    // by the original PHP include file and return the value from Database if any.
    
    echo strlen($val);
    // returns the length count of the Field Tag itself 
    // even if there is or isn't any value for this tag in the database.
    // hence it couldn't be parsed as an empty element..
    
    echo $val
    // returns the correct value of this field from the database 
    // and would return blank if no value.


    Last attempt, If I'm unable to understand why is it parsing the Field Tags as a string itself instead of the returned value of the field tag, then I'm back to using jQuery again

    Any suggestions?
    Last edited by thaidatelink; 02-28-2010 at 12:38 PM.

  4. #14
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's possible to do this using Regex, and even with different characters, but the pattern matching will get complex.

    The easiest way to do this is to fix the original script, as I said before.

    It is much harder to modify existing HTML than to just fix it in the first place.

    If you want to try to modify the existing html, that's fine, but I think you'd be able to fix the original code faster-- all you have to do is find it and add an "if (!empty) {...." to the beginning of that section. ("!empty" meaning whatever you are checking is empty-- probably info from the database.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    thaidatelink (03-07-2010)

  6. #15
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    Finally did it

    Here is how I did it...in case it may be of interest to others.

    Code:
    // Function to get the current page URL //
    function curPageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    
    // $curPageURL to call for the current URL string later //
    $curPageURL = curPageURL(); 
    
     // Get the referrer //
    $curPageReferer = $_SERVER['HTTP_REFERER'];
    
    // If referrer is from cURL, load the template source code //
    if ($curPageReferer == "SpecificReferrerNameHere") {
    
    echo '
    
    // Code of the whole page goes here //
    
    ';
    
    } else {
    
    // If the referrer is not from cURL as specified //
    // Start the buffer //
    
    ob_start();
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curPageURL); // Load the current page into the buffer //
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_REFERER, "SpecificReferrerNameHere"); // Referrer must be the same as specified above //
    $contents = curl_exec ($ch);
    curl_close ($ch);
    
    // Reason I load the current page is because the template tags wouldn't be parsed as it's original value until it's been output to the browser. Therefore, this is the only method I could get the output html source code and load it into the buffer to be modified further // 
    
    // include the Simple HTML DOM Parser Script //
    // Can be found here -> http://simplehtmldom.sourceforge.net //
    require('php/simple_html_dom.php');
    
    // Get the data from cURL //
    $data = str_get_html($contents)
    
    // Get the specific Div from the data //
    $html = $data->getElementById("listing_details_container");
    
    // Use Simple HTML Dom parser to remove all the empty span, div, ul, li, etc. //
    foreach($html->find('comment') as $comment){
    	$comment->outertext = '';
    }
    foreach($html->find('del') as $del){
    	if(Trim($del->innertext) == '') $del->outertext = '';
    }
    foreach($html->find('span span span') as $xxxspan){
    	if(Trim($xxxspan->innertext) == '') $xxxspan->outertext = '';
    }
    foreach($html->find('span span') as $xxspan){
    	if(Trim($xxspan->innertext) == '') $xxspan->outertext = '';
    }
    foreach($html->find('span') as $xspan){
    	if(Trim($xspan->innertext) == '') $xspan->outertext = '';
    }
    foreach($html->find('li') as $li){
    	if(Trim($li->innertext) == '') $li->outertext = '';
    }
    foreach($html->find('ul') as $ul){
    	if(Trim($ul->innertext) == '') $ul->parent->parent->outertext = '';
    }
    
    // Output the modified HTML //
    echo $html;
    
    ob_end_flush();
    };
    I'm sure this isn't the best method...but it gets the job done

    Hopefully there isn't any security implications

    Again, a BIG THANKS to djr33 and traq for your precious input.

    Note: Becareful with the code above...if you happen to load the template source code while initiating cURL function to return the template source code at the same time would cause a permanent loop and would crash your server; crashed my dedicated server twice before I got it right :P
    Last edited by thaidatelink; 03-07-2010 at 03:51 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •