SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Help with regex

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with regex

    Hi All,

    I'm wondering if there's a way to do the following:

    I have a load of text which may contain a 'tag'. The tag will always be braced between two hash marks...
    PHP Code:
    $body "My text goes here and next is a 'tag' #sayhello#. That was it folks."
    I would like to extract the tag from the $body, run a function based on the tag name (eg. sayhello() ) and replace the tag with the output from the function.

    Running the function is fine. My problems are:
    1. How to I extract the tag name from $body?
    2. How to replace it with the contents of another var (outputted from the function)?
    3. What if there is more than 1 tag in the string?

    I'm sure this is possible, but I need a bit of a head start with it. Would appreciate any help.

    Cheers,
    Af.

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple and secure solutions...

    Simple:
    PHP Code:
    $body "My text goes here and next is a 'tag' #sayhello#. That was it folks.";
    echo 
    preg_replace('/#([^#]+)+#/e''\\1()'$body);

    function 
    sayhello() {
      return 
    'something else';

    As you can see, someone (with access to $body) could call ANY founction.

    This one is more secure. You only parse predefined "tags".
    PHP Code:
    $body "My text goes here and next is a 'tag' #blabla# or #sayhello#. That was it folks.";
    echo 
    preg_replace('/#([^#]+)+#/e''TagProcessor(\'\\1\')'$body);

    function 
    TagProcessor($name) {
      switch (
    $name) {
        case 
    'sayhello':
          return 
    'something else';
        case 
    'blabla':
          return 
    'BLABLA';
        default:
          return 
    null;
      }


  3. #3
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent!!

    I'm using it in a type of content management system where the user can call special function (eg. #showTimetable#) to generate dynamic content.

    In that instance, would you still recommend explicitly switching the function calls? I guess that does allow for some error checking too.

    Thanks again.
    Af.

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay - almost worked!
    Code:
    Call to undefined function: ffffcc\">
    Looks like it's finding color="#ffffcc" tags and trying to replace them! If it helps, function names will only contain alpha characters.

    Af.
    Last edited by Afro Boy; Oct 1, 2003 at 23:53.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for that... Try with this then:
    preg_replace('/#([A-Za-z0-9]+)
    +#/e', 'TagProcessor(\'\\1\')', $body);

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic! Thanks for this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •