simple preg_match problem

Discussion in 'PHP' started by squishi, Dec 28, 2008.

  1. #1
    $string = "<!-- Start table -->Some text here<!-- End table -->";
    
    $search_regex='/<!-- Start table -->(.*)<!-- End table -->/';
    
    $result = preg_match($search_regex, $string, $match);
    
    echo count($match); //is 0!
    
    PHP:
    I cannot get this very simple preg_match to work. :(
    Any help, please? :confused:
     
    squishi, Dec 28, 2008 IP
  2. hackfanatic

    hackfanatic Peon

    Messages:
    29
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i have used your exact same code.. got output as "2" !?!
     
    hackfanatic, Dec 28, 2008 IP
  3. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #3
    What you actually want to do ?
     
    harrisunderwork, Dec 28, 2008 IP
  4. squishi

    squishi Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hmm, zes, that is correct.
    I simplified it. The string contains a lot of html, and it does indeed return zero. I am sure it contains the comments above. :(
     
    squishi, Dec 28, 2008 IP
  5. squishi

    squishi Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, here it is outputting 0:

    <?php
    
    $string="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
    <HTML>
    	<HEAD>
    	</HEAD>
    	<body>
    										<!-- Start table -->
    TEST
    										<!-- End table -->
    	</body>
    </HTML>";
    
    $search_regex='/<!-- Start table -->(.*)<!-- End table -->/';
    
    $result = preg_match($search_regex, $string, $match);
    
    echo count($match); //is 0!
    
    ?>
    PHP:
    i don't understand why it doesn't work... :(
     
    squishi, Dec 28, 2008 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    $search_regex='/<!-- Start table -->\s*(.+?)\s*<!-- End table -->/';
    Code (markup):
     
    crazyryan, Dec 28, 2008 IP