|
Posted by cendrizzi on 10/30/06 00:42
It's not for validation. It's for some custom template stuff that
tells my stuff where to store the value of the form element in the
session. That may not make sense but it's what I need for my
application. So I use the ob_start, etc functions and use regular
expressions against the buffer to manipulate the html or change the
behaivor of certain elements. I could just get the name of each
element and check them using strpos or strstr for the '{' character but
I hoped I could use RE to check from the start if it had that so it
wouldn't require the extra string searches.
Hope that makes sense, it's always a bit of a challenge to explain
things clearly, especially if the program is quite a big one.
On Oct 29, 4:17 pm, Pedro Graca <hex...@dodgeit.com> wrote:
> cendrizzi wrote:
> > So I start with this:
> > /<(input|select|textarea)[^>]*name\s*\=\s*\"[_a-zA-Z0-9\s]*\"[^>]*>/You'd better not use regular expressions to validate HTML.
> The following line is perfectly valid HTML (I think in any version)
>
> <input type="text" name="x><y" id="xy">
>
> > but need to modify it so it only matches if it has '{' characters in
> > the name but to not match if it does not.
>
> > So this would not match:
> > <input name="test">
>
> > But this would match:
> > <input name="test{0}">Get the name. Verify it has '{' and '}' (in that order and once only?)
>
> <?php
> $name = get_name('<input name="test{0}">'); // 'test{0}'
> if (name_is_valid($name)) {
> // whatever
> }
>
> function get_name($html) {
> return 'test{0}'; // sorry!
> }
>
> function name_is_valid($name) {
> if (($p1 = strpos($name, '{')) === false) return false;
> if (strpos($name, '{', $p1+1) !== false) return false;
> if (($p2 = strpos($name, '}')) === false) return false;
> if (strpos($name, '}', $p2+1) !== false) return false;
> return $p1 < $p2;
> }
> ?>
>
> --
> I (almost) never check the dodgeit address.
> If you *really* need to mail me, use the address in the Reply-To
> header with a message in *plain* *text* *without* *attachments*.
Navigation:
[Reply to this message]
|