SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Preg_match

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preg_match

    I am kinda beginner in regular expressions.

    Could anyone help me out? I wanna get as an output "Stephen" word.

    Code:
    <td width=20%>Name:</td><td>Stephen <div style="float: right"></div></td>

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match("#<td width=20%>Name:</td><td>([\w^<]+)#"$search_string$matches); 
    Didn't test it, might not work.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately , does not work.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str '<td width=20%>Name:</td><td>Stephen <div style="float: right"></div></td>';

    preg_match("#<td width=20%>Name:</td><td>([\w^<]+)#"$str$matches);

    var_dump($matches); 
    Works perfectly fine for me.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right.

    For this spare of code it works.
    But im fetching first:

    Code PHP:
     
    preg_match(
          "@<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=100%>
           <TR BGCOLOR=#505050><TD COLSPAN=2 CLASS=white>
           <B>Informations</B>.+?</TABLE>@is", $website, $matches );
        $getinfo = $matches[0];


    $website is a variable of the site I am connecting to.

    Then I am trying to fetch the data included in this table.

    Code PHP:
    preg_match("@<td width=20%>Name:</td><td>([\w^<]+)@", $getinfo, $matches);

    Maybe 1st preg_match should be fixed?

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your regular expression (the 1st one) won't capture anything in the $matches variable since you're not using any () to actually put the contents of the matched string inside a variable.
    Also, you need $matches[1], not $matches[0].

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How it should be fixed then?

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2010
    Location
    Arizona
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are parsing complex HTML, take a long, hard look at Simple HTML DOM instead of writing regular expressions:

    http://simplehtmldom.sourceforge.net/

    Tables without classes do present a problem for selectors but is still a lot easier to navigate than parsing HTML yourself.
    Thomas Hruska

    Single Sign-On Server/Client - The PHP login system that rocks.

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems very useful , need only explore some functions I need.


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
  •