SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression Help

    I keep getting an empty array back when trying to pull data from a string.

    The data looks like this exactly, with spacing and all:

    <div class="date">
    <span id="header">Monday, April 09, 2007</span></div>

    Im trying to get everything between the div tags but to no avail. Here is the code Im using:

    PHP Code:
     
    preg_match
    ('#<div class="date">(.*)</div>#im',$string,$currdate); 
    Any suggestions? I know Im missing something simple here.

    Thanks
    Silly

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string='<div class="date">
    <span id="header">Monday, April 09, 2007</span></div>'
    ;
    preg_match('#<div class="date">(.*)</div>#ism',$string,$currdate);
    echo 
    $currdate[0]; 
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, it's probably your darn newline. I've had many a problem with newlines and return characters when doing regular expressions. You can probably run your string through the str_replace(array("\n","\r"), '', $yourString);

    Ernie1 what is the #s for?

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The #, or whatever you choose, marks beginning and the end of the pattern, however, if you used the slash as starting or ending delimiter, you would have to escape the special characters.
    /i makes the regex match case insensitive
    /s enables single-line mode
    /m enables multi-line mode
    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks. I always use a / for my beginning and end. I figured that is what it was for. I didn't know what the s and m stood for. This is great news! I'll be using the /sm all over the place from now on. Awesome.


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
  •