I've been trying to get a function that simply recognises postcode patterns within a page of text, then put

<postcode>N19 3QN</postcode>

tags around it. I've tried all the examples online, including string replace functions and ereg etc, but can't seem to get it working..

Alternatively does anyone know a good plain English tutorial on this type of thing.

Many thanks in advance.

    At its dumbest, you would do something like this:

    <?php
    $text = 'This is some text with the post code N19 3QN embedded in it.';
    $result = preg_replace('/(N19 3QN)/', '<postcode>$1</postcode>', $text);
    echo htmlspecialchars($result);
      laserlight;10973922 wrote:

      At its dumbest, you would do something like this:

      <?php
      $text = 'This is some text with the post code N19 3QN embedded in it.';
      $result = preg_replace('/(N19 3QN)/', '<postcode>$1</postcode>', $text);
      echo htmlspecialchars($result);

      if the format is always the same (with letters and numbers) try something like this for catching all postcodes:

      <?php
      $text = 'This is some text with the post code N19 3QN embedded in it.';
      $result = preg_replace('/([A-Za-z]{1}\d{2} \d{1}[A-Za-z]{2})/', '<postcode>$1</postcode>', $text);
      echo htmlspecialchars($result);

        Thanks for the replies so far.

        Desdinova: That's roughly where I'd got up to, but I'm finding that postcodes can either be N21 , NW21 and so on.. I'm getting confused with the string codes to be honest. When I look at

        '/([A-Za-z]{1}\d{2} \d{1}[A-Za-z]{2})/

        my head spins a little.. Don't know why, I can do pretty much everything else with php 🙁

          Note that Desdinova's suggested regex pattern can be simplified to /([A-Za-z]\d{2} \d[A-Za-z]{2})/

          Anyway, you need to know what is the pattern you are looking for, and be aware that there may be false positives.

            Thanks Laserlight. I've been keeping a close eye on false positives, as this would cause major issues with my current site. The pattern I need is almost identical to this:

            code=html|(TDCU1ZZ)|((([A-PR-UWYZ][0-9][0-9]?)|"
            ."(([A-PR-UWYZ][A-HK-Y]][0-9][0-9]?)|"
            ."(([A-PR-UWYZ][0-9][A-HJKSTUW])|"
            ."([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))"
            ."[0-9][ABD-HJLNP-UW-Z]{2})[/code]

            Which I found on another website, but didn't really solve my problem as it simply checks a form input for valid postcode, not text in a website.

            Appreciate your help btw.

              haywardgb wrote:

              The pattern I need is almost identical to this: (...) Which I found on another website, but didn't really solve my problem as it simply checks a form input for valid postcode, not text in a website.

              Show us the code that incorporates the pattern that you tried to use. Show also the sample input, expected output and actual output.

                Just pasted the regex you found in the preg_replace. How's that working for you?

                <?php
                $text = 'This is some text with the post code N19 3QN embedded in it.';
                $result = preg_replace("/(GIR0AA)|(TDCU1ZZ)|((([A-PR-UWYZ][0-9][0-9]?)|"
                ."(([A-PR-UWYZ][A-HK-Y]][0-9][0-9]?)|"
                ."(([A-PR-UWYZ][0-9][A-HJKSTUW])|"
                ."([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))"
                ."[0-9][ABD-HJLNP-UW-Z]{2})/", '<postcode>$1</postcode>', $text);
                
                echo htmlspecialchars($result);
                  Write a Reply...