How to exclude from string all the text inside <ul></ul> tags with PHP

Discussion in 'PHP' started by Kyriakos, Dec 17, 2012.

  1. #1
    Hi,

    I want to exclude from appearing the text inside <ul></ul> tags from a string with PHP.

    is this possible?

    how i can do it?

    thanks in advance.
     
    Kyriakos, Dec 17, 2012 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Here you go
    
    <?php
    $string = 'This is a test text. <ul><li>item 1</li><li>item 2</li><li>item 3</li><li>item 4</li></ul> There shouldn\'t be any lists anymore.';
    $pattern = '/(<ul>.*?<\/ul>)/i';
    $replacement = '';
    echo preg_replace($pattern, $replacement, $string);
    ?>
    
    PHP:
     
    stephan2307, Dec 17, 2012 IP
  3. motyar

    motyar Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #3
    Simply use stip_tags. :)
     
    motyar, Dec 18, 2012 IP
  4. m_Ch

    m_Ch Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    of course it works: $new_text=strip_tags($text);
     
    m_Ch, Dec 18, 2012 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    strip_tags removes all the tags, but not the text between any specific tags. Use preg_replace or create the string as 2 (or 3) separate strings, build the list and you still have the text not within the <ul></ul> as strings.
     
    Rukbat, Dec 20, 2012 IP