#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Handle multi row replace


    PHP Code:
        preg_match_all('/rowspan=3 class=\"col1\">(.*)#/'$insertData1$matchesPREG_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?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,071
    Rep Power
    9398
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0
    PHP 5.3.5 on WAMP
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,071
    Rep Power
    9398
    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$matchesPREG_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;

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0
    TY, the IF stmt you added does exactly what I needed.

IMN logo majestic logo threadwatch logo seochat tools logo