SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist Tecknowjnkie's Avatar
    Join Date
    Nov 2004
    Location
    Mission Viejo
    Posts
    403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression found in string out to variable

    I have a string in which I am seraching for this

    PHP Code:
    src=../img/[0-9]{3}/[0-9]{2,}. 
    When the expression is valid or found I want it to become a variable i can place re-use.

    This is not working

    $row = "<tr>\n<td>\t\n<img src=\"phpThumb.php?src=$folder/$current&w=100\"/><br />\n" . $form . "\n</td>";
    $img = ereg('src=../img/[0-9]{3}/[0-9]{2,}.', $row);
    echo $row;

    Sorry could not wrap in code - looks for the img src=

    Now in the form vaiable I call out to the $img and it returns nothing

    Thank you for replies

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    regex

    It looks like the regex is working fine. If it's looking for something like
    src=anything anything /img/###/## anything
    Then something like this line
    src="phpThumb.php?src=img/###&w=100"
    doesn't match, so you get nothing.

  3. #3
    SitePoint Evangelist Tecknowjnkie's Avatar
    Join Date
    Nov 2004
    Location
    Mission Viejo
    Posts
    403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Mittineague

    Thank you for reply..

    Here is part of the string $row

    <img src="phpThumb.php?src=../img/064/04.gif&w=100"/>

    There is a bunch of form code before this line and a little form code afterwards but what I need to extract is the actual numbered directory and the numbers.gif out of the string

    for example I need to know from the above line 064/04 I dont care about anything before or after. I have an directory with 100000's of stickers, too much to import data into DB :-)..... but I have a legend that tells me the sticker that was ordered based on those numbers.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    Regex

    If you know what is common then you should be able to get the numbers.
    Unescaped periods in regex mean "any character". But if you know that every source looks like this
    /img/064/04.gif&w=100
    then a regex something like this might work
    "~/img/(0-9){3}/(0-9){2}\.gif~"
    then use matches[1] and matches[2] to get the values.

  5. #5
    SitePoint Evangelist Tecknowjnkie's Avatar
    Join Date
    Nov 2004
    Location
    Mission Viejo
    Posts
    403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey M

    I do not understand this? >
    then use matches[1] and matches[2] to get the values.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    Regex

    Sorry, I should have put "$"s in front of the array vars.
    From the PHP docs, the syntax for ereg is
    int ereg ( string pattern, string string [, array regs])
    and
    Returns TRUE if a match for pattern was found in string, or FALSE if no matches were found or an error occurred.
    So for this line
    PHP Code:
     $img ereg('src=../img/[0-9]{3}/[0-9]{2,}.'$row); 
    $img will be TRUE if the pattern is found in $row.
    But if you write the line like this
    PHP Code:
     ereg('src=../img/[0-9]{3}/[0-9]{2,}.'$row$matches); 
    Then
    If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs, the matches will be stored in the elements of the array regs. $regs[1] will contain the substring which starts at the first left parenthesis; $regs[2] will contain the substring starting at the second, and so on. $regs[0] will contain a copy of the complete string matched.

  7. #7
    SitePoint Evangelist Tecknowjnkie's Avatar
    Join Date
    Nov 2004
    Location
    Mission Viejo
    Posts
    403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You M - I got it working with your help.


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
  •