Results 1 to 3 of 3

Thread: Regular Expressions

  1. #1

    PHP: Regular Expressions

    I am currently working on a website that uses PHP to search a database for a part number. A part of the code uses regular expressions to make it easier to search the text file for the appropriate match. However, I am stuck using a method that seems overburdensome, but I cannot find a means of doing what I want with regular expressions by searching google and other places for php help. So here I am.

    The code I am writing now, looks like this:

    PHP Code:
    if ( ( $number == ) && ( $size == "2inch" ) )
    {
      function 
    type1($type1)
      {
      return (
    eregi('2"'$type1));
      }
        if ( 
    $type == "cht" )
        {
        function 
    type2($type2)
        {
        return (
    eregi("cht"$type2));
            }
      }
        elseif (
    $type == "egt" )
        {
        function 
    type2($type2)
        {
        return (
    eregi("egt"$type2));
            } 
        }

    This will continue for a very long time, egt and cht are types of gauges that are being searched for, and there are 4 different number sets, each with 5 different sizes, each containing about 15 or so gauge types. The work would be outrageous if I had to continue in this fashion.

    What I want to be able to do instead, would be to directly input the type variable into the regular expression search, but when i try to apply this method it doesn't work.

    The conceptual method looks like this:

    PHP Code:
    if ( ( $number == ) && ( $size == "2inch" ) )
    {
      function 
    type1($type1)
      {
      return (
    eregi('2"'$type1));
      }
      function 
    type2($type2)
      {
      return (
    eregi($type$type2));
      }

    Is there any way to implement a variable into a regular expression, like I want above. I have tried putting it inside of quotation marks and apostrophes as well.

    Thanks for any help
    For where the instrument of intelligence is added to brute power and evil will,
    mankind is powerless in its own defense.

  2. #2
    here is what i gathered from that post:
    $number, $size, and $type are passed from a form that the user submits.

    can you please post an excerpt from your database? i assume you are looking through the lines of a text file for a line that matches.

    can you please change "type1" and "type2" to names that are more descriptive? i do not understand what they are really doing.
    if you choose not to decide you still have made a choice!

  3. #3
    i fixed the problem, which was rather stupid

    i was posting the functions outside of a loop that read the lines out of a text file one by one

    i thought that the lines could be sent to the outside function and be fine, but apparently i was wrong.

    once i put the code on the inside of the loop it worked correctly. if i kept it outside it gave me an eregi_empty error
    For where the instrument of intelligence is added to brute power and evil will,
    mankind is powerless in its own defense.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Regular Expressions
    By GSHelpBoy in forum Server-side Forum
    Replies: 7
    Last Post: 6th May 2002, 11:45
  2. regular expressions
    By kiran in forum Client-side forum
    Replies: 1
    Last Post: 19th April 2002, 20:36
  3. Regular Expressions
    By GSHelpBoy in forum Server-side Forum
    Replies: 2
    Last Post: 18th April 2002, 14:49
  4. regular expressions
    By diades in forum Client-side forum
    Replies: 5
    Last Post: 17th April 2002, 20:49
  5. JSP Regular Expressions
    By bdale in forum Server-side Forum
    Replies: 0
    Last Post: 19th November 2001, 13:53

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
  •