Topic: Regex Help in PHP (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=28325" title="Pages that link to Topic: Regex Help in  PHP (Page 1 of 1)" rel="nofollow" >Topic: Regex Help in  PHP <span class="small">(Page 1 of 1)</span>\

 
H][RO
Paranoid (IV) Inmate

From: Australia
Insane since: Oct 2002

posted posted 08-12-2006 08:16

Hi all, trying to do some formatting on some existing code...

I want to remove the id from all forms on a page.

There are two cases, one where the id tag has no spaces and is not surrounded in quotes, the other where it is. Some cases have titles and actions in the form tags also.


code:
Case 1
<form id="200681 21520 27; some stuff"  method="post">

Case 2
<form id=2006812152027somestuff  method="post">



How do i make sure i only remove the id from the form elements and not all elements, and any help with the regex would be appreciated.. thanks!

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 08-12-2006 18:15

Why not run two different regexes

<form (id="[^"]+")\s+method=

and

<form id=\d+\S+\s+method=

With vim you could run the following two regexes to rid youself of the unwanted id.

%s/<form id="[^"]\+"\s\+method=/<form method=/g

%s/<form id=\d\+\S\+\s\+method=/<form method=/g

Dan @ Code Town

H][RO
Paranoid (IV) Inmate

From: Australia
Insane since: Oct 2002

posted 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>'




Im a bit confused with how to match spaces, Is that the \s you have?

Will have another crack at it with your solutions, thanks.

H][RO
Paranoid (IV) Inmate

From: Australia
Insane since: Oct 2002

posted 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);




Do the first set of brackets () around the ID serve any purpose? Is there another use apart from backreferencing?

Thanks WM

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 08-13-2006 04:08
code:
<form\s+id=("?)([^"]+|\S+)\1\s+method="post">

id contents are in $2

RegExLib Cheat Sheet: http://regexlib.com/CheatSheet.aspx
I use it all the time when using advanced regex, very helpful for beginners IMO.

(Edit: Too bad i didn't read the first post very well... perhaps this will help someone else.)



(Edited by zavaboy on 08-13-2006 04:11)

H][RO
Paranoid (IV) Inmate

From: Australia
Insane since: Oct 2002

posted posted 08-13-2006 08:35

Hmm wouldnt the id contents be in 1? and the rest of it in 2?

Thanks will check out that link.

I find this site pretty good and normally gets me through:

http://www.regular-expressions.info/



Been working 17 hours straight, and at the end of the day regex isnt the easiest thing to get your head around ;/

divinechaos
Bipolar (III) Inmate

From:
Insane since: Dec 2001

posted 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));



Hope that helps,
DC

H][RO
Paranoid (IV) Inmate

From: Australia
Insane since: Oct 2002

posted posted 08-13-2006 10:37

Thanks all, hard to find examples like these

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 08-13-2006 21:09

Just thought I'd point this out:

[^\s]+ is exactly the same as \S+



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu