Results 1 to 6 of 6

Thread: confusing preg_match_all function

  1. #1

    Thread Starter
    Addicted Member thamizhinpan's Avatar
    Join Date
    Dec 2005
    Location
    TE
    Posts
    243

    confusing preg_match_all function

    HTML Code:
    <table >
      <tr>
        <td></td>
      </tr>
      <tr>
        <td class=\"Contents\">
    		<p>Large Paragraph1....</p>
    		<p>Large Paragraph1....</p>
    		<p>Large Paragraph1....</p>
    		<p>Large Paragraph1....</p>
    	</td>
      <tr>
        <td class=\"Contents\">
    		<p>Large Paragraph 2</p>
    		<p>Large Paragraph 2</p>
    		<p>Large Paragraph 2</p>
    		<p>Large Paragraph 2</p>
    		<p>Large Paragraph 2</p>
    	</td>
    
      </tr>
    </table>
    I want to get contents of cell which contains ID as MainCell.
    So I have wrote a sample php code like this
    PHP Code:

    <?php $html="<table >
      <tr>
        <td></td>
      </tr>
      <tr>
        <td class=\"Contents\">
            <p>Large Paragraph1....</p>
            <p>Large Paragraph1....</p>
            <p>Large Paragraph1....</p>
            <p>Large Paragraph1....</p>
        </td>
      <tr>
        <td class=\"Contents\">
            <p>Large Paragraph 2</p>
            <p>Large Paragraph 2</p>
            <p>Large Paragraph 2</p>
            <p>Large Paragraph 2</p>
            <p>Large Paragraph 2</p>
        </td>

      </tr>
    </table>"
    ;


        
    preg_match_all('#<td class=\"Contents\">(.+?)</td>#'$html$MainHeads);


    echo 
    "<table>";
        foreach (
    $MainHeads[1] as $Contents
        {
        echo(
    "<tr>");
        echo(
    "<td>.$Contents.</td>");
        echo(
    "</tr>");
        }
    echo 
    "</table>";
    ?>
    But this is not working, What's wrong on this code?



    Note: When I change $html like this

    PHP Code:
    $html="<table >
      <tr>
        <td></td>
      </tr>
      <tr>
        <td class=\"Contents\"><p>Large Paragraph1....</p></td>
      <tr>
        <td class=\"Contents\"><p>Large Paragraph 2</p></td>
      </tr>
    </table>"

    It is working
    If above question or answer will help to you
    Don't forget to rate me
    தமிழ்இன்பன்

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: confusing preg_match_all function

    Your regular expression is not meant to have hashes in "#" and also, you don't need to use preg_match_all. Use the Dom Extension.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: confusing preg_match_all function

    Quote Originally Posted by visualAd
    Your regular expression is not meant to have hashes in "#"
    I use hashes. Lots of characters can be used as start and end tokens.


    The expression doesn't work because you've unnecessarily escaped the double quote characters. These do not need to be escaped in single-quoted string literals.

    However, I would use the DOM (as advised) and an XPath query.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: confusing preg_match_all function

    Quote Originally Posted by penagate
    I use hashes. Lots of characters can be used as start and end tokens.
    I have never seen this usage. I've always assumed it as //
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: confusing preg_match_all function

    http://au2.php.net/manual/en/ref.pcre.php

    The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash.

  6. #6

    Thread Starter
    Addicted Member thamizhinpan's Avatar
    Join Date
    Dec 2005
    Location
    TE
    Posts
    243

    Re: confusing preg_match_all function

    But i have got the correct patten for need from PHP manual.
    I agree '#' was a cause for my problem. And another cause was that cell contains multi line style. So i want to put /m on end
    But I have found correct patten and modify it as advances .
    That patten is
    [
    "/<td\s+.*?class=[\"\']".$Contents."[\"\']?[^>]*>(.+?)<\/td>/ism
    If above question or answer will help to you
    Don't forget to rate me
    தமிழ்இன்பன்

Posting Permissions

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



Click Here to Expand Forum to Full Width