Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 16th, 2009, 06:25 PM
ignas2526 ignas2526 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 6 ignas2526 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 35 m
Reputation Power: 0
Question Need help with preg_replace

Hello,
I trying to add checked="checked" to the input which value x.
find '<input'.anything here.'value="'.value.'"'.anything
replace '<input' with <input checked="checked"

here is what I end up with:
Code:
//text in which preg_replace must find input
$v = 'some text<input name="something1" type="checkbox" value="123" />something here<br><br><input name="something2" type="checkbox" value="122" /><br>';
$output = preg_replace('/<input(.*?value="123" .*?)\s/','<input checked="checked"$1',$v,1);
echo $output


result:
some text<input checked="checked" name="something1" type="checkbox" value="123" />somethinghere<br><br><input name="something2" type="checkbox" value="122" /><br>
expected result:
some text<input checked="checked" name="something1" type="checkbox" value="123" />something here<br><br><input name="something2" type="checkbox" value="122" /><br>

Any ideas how to fix that?
Thanks.

Reply With Quote
  #2  
Old June 16th, 2009, 06:37 PM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 737 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 22 h 50 m 16 sec
Reputation Power: 928
You're problem is the regex has a \s on the end that isn't included in the capture, so it is removed from the string and not replaced.

A better solution is not to match more than you care about. The look ahead makes sure that value="123" appears, but doesn't include it in the match.
Code:
/<input(?=.*value="123")/


The best solution would be to parse the markup and make your changes to the tree structure. The regex will get confused with input like:
Code:
<input/> value="123"
<input value="wrong"/> <input value="123"/>
__________________
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

Reply With Quote
  #3  
Old June 16th, 2009, 07:28 PM
ignas2526 ignas2526 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 6 ignas2526 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 35 m
Reputation Power: 0
Thank you for quick reply, well the value will be only filled with inputs so markup will be clean, however speed is important, i just found what
preg_replace('|value="'.$i.'"|','value="'.$i.'" checked="checked"',$v,1);
is three times quicker than
preg_replace('/<input(?=.*value="123")/','<input checked="checked"$1',$v,1);

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Need help with preg_replace

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap