Web Hosting Forums

Results 1 to 5 of 5

This is a discussion on regxp trouble in the Hosting Talk & Chit-chat forum
...

  1. #1
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74

    regxp trouble

    I have s php script:

    PHP Code:
    <?
    $string 
    "C16 AAAŁ399<abcd-efghijklmnopqrstuvwxyz123>E-mail a FriendD 16 AAAŁ499<abc
    defghijklmnopqrstuvwxyz123>E-mail a FriendG16 AAAŁ499<abcdefghijklmnopqrst
    uvwxyz123>E-mail a FriendN16 AAAŁ250<abcdefghijklmnopqrstuvwxyz123>E-mail
    a FriendP16 AAAŁ250<abcdefghijklmnopqrstuvwxyz123>E-mail a FriendT16 AAAŁ
    250<abcdefghijklmnopqrstuvwxyz123>E-mail a FriendV16 AAAŁ399<abcdefghijkl
    mnopqrstuvwxyz123>E-mail a FriendW16 AAAŁ399<abcdefghijklmnopqrstuvwxyz12
    3>E-mail a FriendX16 AAAŁ499<abcdefghijklmnopqrstuvwxyz123>E-mail a Friend
    Y16 AAAŁ499<abcdefghijklmnopqrstuvwxyz123>E-mail a Friend<br><br>"
    ;


    preg_match("^[A-Z].*",$string$regs);
    echo 
    $regs;
    ?>
    I am trying to extract all the numberplates and their prices i.e. C16 AAAŁ399

    and all the others. I figured the only way to do this is with regxps so i stuck all the info above into $script, and then tried to do some regxp'ing. To start off with i tried:

    preg_match("^[A-Z].*",$string, $regs);
    echo $regs;

    I thought this should have worked as the first letter is C, but i got this message in my browser:

    Warning: No ending delimiter '^' found in c:\wampp1-smart\htdocs\sms\regs.php on line 13
    Array

    I have also tried using a dollar sign infront of the asterix, and doing the script without the ^ sign, but nothing works.

    Anyone know why?

  2. #2
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74
    I made a mistake here i have just realised, i9 should have put / behind the first " and / in front of the last / inside of the preg match. However, when i view the code in my browser window all i get is Array.

    Shouldnt it be echoeing the contents of the array, i.e. $string?

  3. #3
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    The third argument to preg_match is a reference to an array where the results of the match will be stored. So, in order to get your match, you'd want to use $string[0].

    Here's how it works:

    In a regexp you can group expressions with parens. When PHP matches text based on your expression it places the entire match in element 0, and the portion of each match inside the parens in sequential elements 1 to n.

    The easist way to illustrate this is with a flatfile database example. Say you have a product listing in a text file with the format of each line something like this:

    productnumber<tab>description<tab>price< newline>

    You could use a regular expression to pull out each part of this line (although, in this case the split() or explode() functions would be a better choice). The expression might look something like this:

    /^([^\t]*)\t([^\t]*)\t(.*)$/

    Which matche any number of non-tab characters, followed by a tab, then any number of non-tab characters again, another tab, and then any characters until the end of the line.

    The third argument to preg_match would look like this after parsing the following line of text:

    The line:
    12345<tab>My Greatest Product<tab>19.95<newline>

    The array:
    [0] -> 12345<tab>My Greatest Product<tab>19.95<newline>
    [1] -> 12345
    [2] -> My Greatest Prooduct
    [3] -> 19.95<newline>

    Hope this helps...

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  4. #4
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74
    yeah thanks that has helped. I found an example:

    PHP Code:
    $str "The quick brown fox jumped over the lazy old dog";

    preg_match_all ("/(^|\s+)(o\w+)($|\s+)/i"$str$matchesPREG_PATTERN_ORDER);


    echo 
    $matches[2][0]; 
    does the s+ stand for one or more spaces?

    what sort of regular expression could i use to get all the plates such as
    D16 AAAŁ499
    in the example above?

    thanks

  5. #5
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    Are you sure the s+ shouldn't be \s+? That would would match one or more spaces. s+ without the backslash in front of it matches one or more s characters and doesn't make a whole lot of sense in the context.

    Assuming that the expression would always be a letter, followed by two numerals, a space, and more letters, a pound sign, and another set of numerals, you could use:

    /([A-Z][0-9]{2}\s[A-Z]{3})Ł([0-9])+/

    In this case, the first () match would contain the plate number and the second would contain the price (without the pound sign).

    Alternatively, you could use
    /([A-Z][0-9]+\s[A-Z]+)Ł([0-9])+/ which would match a letter followed by one or more digits, a space, and ore or more letters.

    I don't know how plate numbers are assigned in the UK, so its hard to explain the best expression to use. In the US every state has thier own format for plates, and most allow you to buy "vanity plates" where you can get anything you want on your plate, wich makes it pretty much impossible to write an expression to pick a plate number out of a page of text.

    You also have to consider what you are using the expression to do. If you are checking to see if someone has entered a valid plate number you need to have a more strict expression than you do if you are searching text for plate numbers that you know are valid (which you seem to want to do). In the two examples I gave, both would find the plate number you gave me, but the first is much more strict than the second. The second would match "A1234567890 ABCDEFG," which is obviously too long to use on a license plate.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

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
  •