The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Handle multi row replace
Discuss Handle multi row replace in the Regex Programming forum on Dev Shed. Handle multi row replace Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 1st, 2011, 08:21 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 18
Time spent in forums: 2 h 24 m 17 sec
Reputation Power: 0
|
|
|
Handle multi row replace
PHP Code:
preg_match_all('/rowspan=3 class=\"col1\">(.*)#/', $insertData1, $matches, PREG_SET_ORDER);
$values=(count($matches)-1);
for ($i = 0; $i <= $values; $i++) {
$insertData1 =preg_replace("/<tr><td rowspan=3 class=\"col1\">(.*?)<\/td>\n<tr><td class=\"col2\">/","<tr><td rowspan=3 class=\"col1\">$1</td>\n<tr>" . $matches[$i][1] . "\",\"", $insertData1,1);
$insertData1 =preg_replace("/<td class=\"col2\">/", $matches[$i][1] . "\",\"", $insertData1,1);
}
This worked fine until one of my test files had a rowspan=5, then it went sideways.
Is there a way to nest the second preg_replace to only fire if the first preg_replace criteria is met?
|

September 1st, 2011, 03:34 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Yes. What version of PHP are you using?
|

September 2nd, 2011, 07:08 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 18
Time spent in forums: 2 h 24 m 17 sec
Reputation Power: 0
|
|
|
PHP 5.3.5 on WAMP
|

September 2nd, 2011, 02:09 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
I find it so comforting that more and more people are using 5.3...
I was originally going to suggest using preg_replace_callback() but then I realized that the second regex string may not be contained within the first.
If the first preg_replace() made a replacement then the string will have changed:
PHP Code:
preg_match_all('/rowspan=3 class=\"col1\">(.*)#/', $insertData1, $matches, PREG_SET_ORDER);
$values=(count($matches)-1);
for ($i = 0; $i <= $values; $i++) {
$temp =preg_replace("/<tr><td rowspan=3 class=\"col1\">(.*?)<\/td>\n<tr><td class=\"col2\">/","<tr><td rowspan=3 class=\"col1\">$1</td>\n<tr>" . $matches[$i][1] . "\",\"", $insertData1,1);
if ($temp != $insertData1) {
$temp =preg_replace("/<td class=\"col2\">/", $matches[$i][1] . "\",\"", $temp,1);
}
$insertData1 = $temp;
}
|

September 6th, 2011, 12:46 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 18
Time spent in forums: 2 h 24 m 17 sec
Reputation Power: 0
|
|
|
TY, the IF stmt you added does exactly what I needed.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|