Topic awaiting preservation: Regex Help in PHP (Page 1 of 1) |
|
---|---|
Paranoid (IV) Inmate From: Australia |
posted 08-12-2006 08:16
Hi all, trying to do some formatting on some existing code... code: Case 1 <form id="200681 21520 27; some stuff" method="post"> Case 2 <form id=2006812152027somestuff method="post">
|
Maniac (V) Mad Scientist From: Rochester, New York, USA |
posted 08-12-2006 18:15
Why not run two different regexes |
Paranoid (IV) Inmate From: Australia |
posted 08-13-2006 03:43
I ended up doing this, but its less than ideal. If the format of my tag changes then it will no longer work. It does however work for either scenario in the one regex, i would rather have 2 that work properly however code: preg_replace('!<form id=.* (.*) (.*) (.*) ?>!i','<form \\1 \\2 \\3>'
|
Paranoid (IV) Inmate From: Australia |
posted 08-13-2006 03:57
Okay i think i have it, here is what i ended up with: code: preg_replace('!<form (id="[^"]+")\s+(.*)>!i','<form \\2>', $strHtmlParsed); $strHtmlParsed = preg_replace('!<form (id=\d+\S+)\s+(.*)>!i','<form \\2>', $strHtmlParsed);
|
Bipolar (III) Inmate From: f(x) |
posted 08-13-2006 04:08
code: <form\s+id=("?)([^"]+|\S+)\1\s+method="post"> id contents are in $2 |
Paranoid (IV) Inmate From: Australia |
posted 08-13-2006 08:35
Hmm wouldnt the id contents be in 1? and the rest of it in 2? |
Bipolar (III) Inmate From: |
posted 08-13-2006 09:30
A simplified regex for both types, including (working) example code: code: $string = '<form id="200681 21520 27; some stuff" method="post">\n <form id=2006812152027somestuff method="post">'; echo htmlentities(preg_replace('/<form (id=("[^"]+"|[^\s]+)\s+)(.*)>/i', "<form \\3>", $string));
|
Paranoid (IV) Inmate From: Australia |
posted 08-13-2006 10:37
Thanks all, hard to find examples like these |
Bipolar (III) Inmate From: f(x) |
posted 08-13-2006 21:09 |