Activity Stream
48,167 MEMBERS
6764 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 2 12 LastLast
Results 1 to 10 of 16
  1.     
    #1
    Member

    Default A doubt about php's preg_match result.

    So, I have this html file that basicaly says:

    PHP Code: 
    echo '<table>
    <tr>
         <td>Welcome to:</td>
         <td><strong>Hell</strong></td>
    </tr>
    </table>'

    (whether this code is or not valid, does not matter.. Is just of the sake of having an example)

    Now, I have another php file that (should) gets (through CURL and preg_match) the "<strong>Hell</strong>" part:

    Curl Result ends in the var: $Result
    Pattern I want to match against the $Result: $pattern = '/ <td>Welcome to\:<\/td>
    <td>(.*)<\/td>/m';

    Preg Match result var: $pregResult

    PHP Code: 
    preg_match ($pattern$Result$pregResult
    doing a var_dump($pregResult); returns an empty array
    PHP Code: 
    array(0) { } 
    yet, testing my example and reg expr in the site http://www.spaweditor.com/scripts/regex/index.php it returns the expected value (<strong>Hell</strong>)

    So, can anyone point me to what is going on wrong because I am out of ideas.

    My setup is windows + XAMPP with phpversion 5.3.5.

    PC

    Here are the actual php files:
    -------------- test.php ------------
    PHP Code: 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php

    echo '<table width="50%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>Welcome to:</td>
      </tr>
      <tr>
        <td><strong>Hell</strong></td>
      </tr>
    </table>'
    ;

    ?>
    </body>
    </html>
    -------------- curl_teste.php ------------
    PHP Code: 
    <?php
    error_reporting
    (-1);  

    $curlTarget "http://127.0.0.1/multiline/test.php";


    $ch curl_init();
    curl_setopt($chCURLOPT_URL,$curlTargget);
    curl_setopt($chCURLOPT_RETURNTRANSFER,1);

    $result curl_exec ($ch);
    curl_close($ch);

    $patternCoiso '/    <td>Welcome to:<\/td>
        <td>(.*)<\/td>/mi'
    ;

    preg_match($patternCoiso$result$match);


    var_dump($match);

    ?>
    PCManiac Reviewed by PCManiac on . A doubt about php's preg_match result. So, I have this html file that basicaly says: echo '<table> <tr> <td>Welcome to:</td> <td><strong>Hell</strong></td> </tr> </table>'; (whether this code is or not valid, does not matter.. Is just of the sake of having an example) Rating: 5

  2.   Sponsored Links

  3.     
    #2
    Member
    Website's:
    bypassx.com
    I think that ":" not need to be scaped... try with:

    Code: 
    $pattern = '/    <td>Welcome to:<\/td>
        <td>(.*)<\/td>/m';
    Not enougth time to test bymyself now....

  4.     
    #3
    Respected Member
    The Regex is fine, your preg_match order is messed up though. It should be:

    PHP Code: 
    preg_match($pattern$Result$pregResult); 
    You first specify the pattern, then the subject and then the name of the array that the results will be put in.

  5.     
    #4
    Member
    bleh.. sorry Loget, that's how I have coded.. I just messed it while retyping in to the post.

    *fixes*

    Meaning that is not the problem :\


    Zeokat: you are right, it's not needed but being scaped won't (or shouldn't) change in anyway the result. i scaped it just to make sure it wasn't that that was messing it up. thanks for the reply though.

  6.     
    #5
    Moderator
    NewEraCracker's Avatar
    any uninitialized variable in code?
    Add this to the beginning of the code for debugging after the <?php tag
    PHP Code: 
    error_reporting(-1); 
    Trusted: Dom, l0calh0st, 0ccul7, robert420
    Find all threads started by NewEraCracker

  7.     
    #6
    Member
    No error output NEC. :\

    I just copied/pasted the actual files to the main post.

  8.     
    #7
    Respected Member
    Here is the code to do exactly what you want:

    PHP Code: 
    $Result '<table>
    <tr>
         <td>Welcome to:</td>
         <td><strong>Hell</strong></td>
    </tr>
    </table>'
    ;
    $w "[\s\S]*?"
    $pattern "/\<td>Welcome to:<\/td\>$w<td\>(.*)<\/td\>/";
    preg_match($pattern$Result$pregResult);
    var_dump($pregResult); 
    $pregResult is :
    Code: 
    array(2) {
      [0]=>
      string(57) "<td>Welcome to:</td>
         <td><strong>Hell</strong></td>"
      [1]=>
      string(21) "<strong>Hell</strong>"
    }

  9.     
    #8
    Respected Developer
    Website's:
    wrzc.org
    You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The &lt;center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n?erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege???x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi?ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c??o??rrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg?<b>ex parsers for HTML will ins[/b]?tantly transport a programmer's consciousness into a world of ceaseless screaming, he comes[s], the pestilent sl[/s]ithy regex-infection wil?l devour your HT?ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi?ght he com?e?s, ?h?i?s un?ho?ly radian?ce? destro?ying all enli??^?ghtenment, HTML tags lea?ki?n?g fr?o?m ?yo??ur eye?s? ?l?ik?e liq?uid pain, the song of re?gular exp?re[s]ssion parsing [/s]will exti?nguish the voices of mor?tal man from the sp?here I can see it can you see _????i^??t???_??_? it is beautiful t?he final snuffing of the lie?s of Man ALL IS LOS????????T ALL I?S LOST the pon?y he comes he c??om[s]es he co[/s][s]me[/s]s the ich?or permeates all MY FACE MY FACE ?h god no NO NOO?O?O NT stop the an?*??????????g????????l??????????e??s<code> ?a???r?????e</code> n?ot re`???a?l~??????? ZA????LG? IS?^??????_ TO???????? TH?E??? ?P???O??N?Y? H?????"????E????`????? ??????_???C???????_??O??????M????????_?E?????????O͇̹̺ͅƝ̴ȳ̳ TH̘?͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ

    Have you tried using an XML parser instead?



    __________________________________________________ __________________
    Doesn't look great here but the original answer to this type of question is the most popular reply ever on the worlds busiest coding site. It's the best answer I've ever seen to a coding question.
    http://stackoverflow.com/questions/1...732454#1732454
    Tutorial How to SEO your Warez Site a guide to help you increase your organic traffic

    Huge list of Warez Sites and free Multiposter Templates

  10.     
    #9
    Member
    Website's:
    EastsideHosting.com MediaONAIR.com ImgWiz.com cPadmin.net EastsidePCWorks.com HostedTalk.com
    PHP Code: 
    preg_match ("/(<h1)(.*?)(>)(.*?)(<\/h1>)/i"$data$match);
    $found $match[4]; 
    That's how I do it...Works good, I wrote a script that scans a download file and parses a specific html tag, in this case <h1 -- Based off some code I use to parse a title of a webpage by getting <title>

    So it's possible.

    Might have to play with $matches (no pun intended) to find the match, 0-w/e prolly be 0-1
    EastsideHosting - (Web Hosting, Proxy Hosting, Reseller Program)
    MediaONAIR - (SHOUTcast Servers, ICEcast Servers, Reseller Program)
    Server Administrator - (Dedicated Servers, VPS Management, One Time Services)
    ImgWiz - (Free Image Hosting)

  11.     
    #10
    Member
    Thanks for your replies guys.

    Lockdown, It's weird but that does not work on my test environment... Am starting to think that the php version I have (or any of its libs) is buggy...

    Mr Happy, I am not a programmer.. am just an entusiast would you point me to the right direction about parsing HTML with a XML parser?

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Looking for Exam Result Script
    By DevilLnC in forum Web Application/Script Support
    Replies: 5
    Last Post: 7th Feb 2014, 02:04 PM
  2. Need help with google search result
    By .nItIsH. in forum Webmaster Discussion
    Replies: 4
    Last Post: 4th Mar 2012, 07:54 AM
  3. preg_match help need!
    By saninokia in forum Web Development Area
    Replies: 8
    Last Post: 27th Nov 2011, 08:39 PM
  4. Why Google Result Going Down!!
    By indianmoviefans.info in forum Webmaster Discussion
    Replies: 13
    Last Post: 23rd Sep 2011, 08:00 AM
  5. Help with preg_match - php !
    By softleaks in forum Webmaster Discussion
    Replies: 5
    Last Post: 21st May 2011, 04:22 PM

Tags for this Thread

BE SOCIAL