Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

A vexing callback challenge...

Last post 08-18-2011, 1:31 PM by ddrudik. 2 replies.
Sort Posts: Previous Next
  •  07-11-2011, 7:22 PM 83505

    A vexing callback challenge...

    Howdy helpful folks...

    I have a large amount of data I need to convert and I'm hoping to build a regex or two to help convert something like this:

    I love-love-love what [User:5138532:dgoldstein] and his [Tag:738533:erongo] kids have done with our [Tag:800657:connected] blog!  (You all should go and leave lots of comments for them!) http://countryconnection.edublogs.org/
        My guys will do some catching up once our exams are out of the way!
        (ping [User:1392073:lisaroberson], [User:1546074:jeanesmith1])

    to this:

    I love-love-love what {user: 14321} and his #erongo kids have done with our #connected blog!  (You all should go and leave lots of comments for them!) http://countryconnection.edublogs.org/
        My guys will do some catching up once our exams are out of the way!
        (ping {user: 2332}, {user: 12332}) 

    the number that follows user: is going to be the result of a query to lookup the old id and replace it with the new one.

    I'm trying to use preg_replace_callback() in php, with limited success.  

    Any help would be very much appreciated.  You've saved my bacon twice before...THANKS!

     

     

  •  07-11-2011, 7:42 PM 83506 in reply to 83505

    Re: A vexing callback challenge...

    I don't have access to a platform that lets me use the PHP callback function, but this may get you on the right path.

    Separating the 2 types of tag, I have used a pattern of:

    \[tag:[^:]*:([^\]]*)\]

    and a replacement string of

    #$1

    with the "ignore case" option set to locate and replace the "Tag:" items; and the pattern of:

    \[user:(\d+)[^\]]*\]

    and the replacement string of:

    {User:$1}

    (again with the "ignore case" option set) to do the same thing with the "user" items - in this case I simply put back the same number to make sure that the rest was working.

    Of course, you can combine these patterns into:

    \[tag:[^:]*:([^\]]*)\]|\[user:(\d+)[^\]]*\]

    (these can be factored but that doesn't change the rest of the processing). Now, if something is captured in match group #1, then you know that this was a "tag" entry and can construct the suitable replacement string in the callback. Similarly, if characters are captured in match group #2, then you can use the digits to perform your lookup and again build the required replacement string.

    The callback function code then becomes a "simple matter of programming" which can be "left as an exercise for the reader".

    Hope this helps

    Susan

     

    PS: The resulting string from the first replacement is:

    I love-love-love what [User:5138532:dgoldstein] and his #erongo kids have done with our #connected blog!  (You all should go and leave lots of comments for them!) http://countryconnection.edublogs.org/
        My guys will do some catching up once our exams are out of the way!
        (ping [User:1392073:lisaroberson], [User:1546074:jeanesmith1])

    and the second replacement is:

    I love-love-love what {User:5138532} and his [Tag:738533:erongo] kids have done with our [Tag:800657:connected] blog!  (You all should go and leave lots of comments for them!) http://countryconnection.edublogs.org/
        My guys will do some catching up once our exams are out of the way!
        (ping {User:1392073}, {User:1546074})

     

  •  08-18-2011, 1:31 PM 83701 in reply to 83505

    Re: A vexing callback challenge...

    <?php
      $str 
    'I love-love-love what [User:5138532:dgoldstein] and his [Tag:738533:erongo] kids have done with our [Tag:800657:connected] blog!  (You all should go and leave lots of comments for them!) http://countryconnection.edublogs.org/ 
        My guys will do some catching up once our exams are out of the way! 
        (ping [User:1392073:lisaroberson], [User:1546074:jeanesmith1])'
    ;
      
    $str preg_replace_callback('/\[([^:]+):([^:]+):([^\]]+)\]/''mycallback'$str);
      echo 
    $str;
      function 
    mycallback($input)
      {
          
    //here you would use $input[1] to access the first capture group etc...
      
    }
    ?>


View as RSS news feed in XML