Greetings
I am trying to automatically create hyperlinks for reports in an agenda. The reports always follow the same formatting, which is "xxx-yy-zz AAA", where xxx is any number from 1-999, yy is a month (1-12), zz is a two digit year, and AAA is a two or three number abbreviation for which committee the report came from. Example 110-5-08 LEJ or 14-11-07 A&O. I want to replace those strings with a hyperlink to the report it references. This is what I have so far:

$pattern = "(\d{1,3}-\d{1,2}-\d{2})";
$replacement = "<a href=\"reportviewer.php?id=$1$2\">$1 $2</a>";
$agenda = "110-5-08 LEJ 1. Law Enforcement & Judiciary....<br>105-12-08 A&O 2. Administrations and Operations Committee ..."
$agenda = preg_replace($pattern, $replacement, $agenda);

Obviously I need a second group for the space and two/three letter abbreviation, but I can't seem to search past the numbers. I can't figure out how to search for a space and those letters, since I need them for the hyperlink. In my example the link should say reportviewer.php?id=110-5-08LEJ and reportviewer.php?id=105-12-08AO respectively. Stripping out the &, if included, is required as well. Any direction would be greatly appreciated.

    could you not just take the xxx-yy-zz AAA part and do a str_replace of ' ' and '&' with ''?

      I can but I still need to find them in the first place. They will appear around 10 times throughout the document.

        Thats what I would have thought. The two or three letters will always be uppercase, so I eliminated the first a-z, however the following code produces nothing:

        $pattern = "(\d{1,3}-\d{1,2}-\d{2})\s([A-Z&]{2,3})";
        $replacement = "<a href=\"boc/reportviewer.php?id=$1\">$1 $2</a>\n";
        $subject = "110-5-08 LEJ 1. Law Enforcement & Judiciary....<br>105-12-08 A&O 2. Administrations and Operations Committee ...";
        echo preg_replace($pattern, $replacement, $subject);
        

        Putting the pattern into the regex tester on supercrumbly works as expected.
        Dropping everything from \s to the right makes it produce output, but it isn't replacing properly. I assume it has something to do with what syntax I'm using vs what my provider provides.

          One problem I can see in your code is that you have no delimiters around your regexp; preg_replace() would be choking with a warning if what's posted is accurate. I'd expect the pattern to be more like

          $pattern = "/(\d{1,3}-\d{1,2}-\d{2})\s([A-Z&]{2,3})/";

            Thats it! I was wondering why if I broke up the syntax to take one word or the other, it would work fine. Apparently the delimiters treat the whole thing as one "word". Thank you very much!

              6 days later

              It has come to my attention that there can be one letter committee designations, so I have changed my code to the following:

              $pattern = "/(\d{1,3}-\d{1,2}-\d{2})\s([A-Z&]{1,3})/"; 
              $replacement = "<a href=\"boc/reportviewer.php?id=$1$2\" target=\"_blank\">$1 $2</a>";
              $agenda = preg_replace($pattern,$replacement,$agenda);
              

              However, this works strangely...A code such as '110-4-07 F' gets replaced as 110-4-07F&, instead of 110-4-07F. A code of 85-3-07 LE&J becomes 85-3-07LEJ. The latter is actually correct, but strange it strips out the & in the latter, but adds it in the former. I would rather not even have to deal with the &, but for some reason I'm told it has to be in there. Do I need to escape the &?

                Write a Reply...