[PHP] Regex and variable usage

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Re: [PHP] Regex and variable usage

    Gerard Samuel <gsam@trini0.org> wrote:
    > Has anyone had any success with using variables in a regex shown below??
    >
    > $foo = 3;
    > $bar = 4;
    > preg_match('/^[a-z0-9\-_\.]+\.[a-z0-9]{$foo, $bar}$/', $some_string)
    You have single quotes, so php wont expand the variables inside.
    >
    > or even
    > preg_match('/^[a-z0-9\-_\.]+\.[a-z0-9]{' . $foo . ', ' . $bar . '}$/',
    > $some_string)
    that should work.

    but you could do this:
    $pattern = "/^[a-z0-9\-_\.]+\.[a-z0-9]{$foo, $bar}\$/";
    preg_match($pattern, $some_string)

    > but this would work
    > preg_match('/^[a-z0-9\-_\.]+\.[a-z0-9]{3,4}$/', $some_string)
    >
    > Thanks for any pointers..
    Curt
    --

    Curt Zirzow Guest

  2. Similar Questions and Discussions

    1. traversing a variable with regex instead of a file
      Hi everyone This is my first posting to the list, although I've been burried knee deep in perl books for some time and have been doing web...
    2. Regex - assign to new variable and replace in one line?
      This is probably simple for the experienced Perl programmer, but as I am learning, and hours of searching has led to nothing, this one has tied me...
    3. Regex capture variable scoping
      Can someone please help me understand what I'm seeing as a contradiction between a statement in the perlre man page and a bug in a script that just...
    4. Regex and variable usage
      Has anyone had any success with using variables in a regex shown below?? $foo = 3; $bar = 4; preg_match('/^+\.{$foo, $bar}$/', $some_string) ...
    5. Usage and Benefit of Table Variable
      Dear all, Two short question for SQL2K: 1) Can table variables be passed into / out of stored procedure as parameters, just in the same way of...
  3. #2

    Default Re: [PHP] Regex and variable usage

    You can use preg_match

    preg_match ( pattern, subject [, matches [, flags [, offset]]])

    where pattern could ve a String variable.

    Source: [url]http://www.webcheatsheet.com/php/regular_expressions.php[/url]
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139