preg_replace and metatags...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul brown

    preg_replace and metatags...

    howdy,

    I have some text:

    This is {search}Pam Grier{/search}.

    What I want is this:

    This is <a href="somepage. php?keyword=Pam Grier">Pam Grier</a>.

    I am having a hard time figuring out a regex to do this using
    preg_replace or ereg_replace.

    Could someone offer me a bit of assistance, please?

    Thanks in advance,

    Paul Brown

  • Tim Van Wassenhove

    #2
    Re: preg_replace and metatags...

    In article <7zHec.21967$3M 2.3718@newssvr2 7.news.prodigy. com>, paul brown wrote:[color=blue]
    > howdy,
    >
    > I have some text:
    >
    > This is {search}Pam Grier{/search}.
    >
    > What I want is this:
    >
    > This is <a href="somepage. php?keyword=Pam Grier">Pam Grier</a>.
    >
    > I am having a hard time figuring out a regex to do this using
    > preg_replace or ereg_replace.[/color]

    You could use strpos to find where {search} and {/search} are.
    And then with substr filter Pam Grier out.

    Looking up wich of the signs in {/search} that have to be escaped, and
    then matching {search}(.*?){/search} would put Pam Grier in $1

    --

    Comment

    • Pedro Graca

      #3
      Re: preg_replace and metatags...

      paul brown wrote:[color=blue]
      > I have some text:
      >
      > This is {search}Pam Grier{/search}.
      >
      > What I want is this:
      >
      > This is <a href="somepage. php?keyword=Pam Grier">Pam Grier</a>.[/color]
      ## Invalid character in URL _______________ ^_
      [color=blue]
      > I am having a hard time figuring out a regex to do this using
      > preg_replace or ereg_replace.
      >
      > Could someone offer me a bit of assistance, please?[/color]

      Try this:

      <?php
      $txt0 = 'This is {search}Pam Grier{/search}.';

      $txt1 = preg_replace('/(.*){search}(.* ){\/search}(.*)/i',
      ## $1 $2 $3
      '$1<a href="somepage. php?keyword=$2" >$2</a>$3', $txt0);

      $txt2 = preg_replace('/(.*){search}(.* ){\/search}(.*)/ie',
      ## evaluate replacement ^
      '\'$1<a href="somepage. php?keyword=\' . urlencode(\'$2\ ') . \'">$2</a>$3\'',
      $txt0);

      echo "txt0 = $txt0\ntxt1 = $txt1\ntxt2 = $txt2\n\n";
      ?>

      --
      USENET would be a better place if everybody read: : mail address :
      http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
      http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
      http://www.expita.com/nomime.html : to 10K bytes :

      Comment

      • Justin Koivisto

        #4
        Re: preg_replace and metatags...

        paul brown wrote:
        [color=blue]
        > howdy,
        >
        > I have some text:
        >
        > This is {search}Pam Grier{/search}.
        >
        > What I want is this:
        >
        > This is <a href="somepage. php?keyword=Pam Grier">Pam Grier</a>.
        >
        > I am having a hard time figuring out a regex to do this using
        > preg_replace or ereg_replace.
        >
        > Could someone offer me a bit of assistance, please?[/color]

        This should work for you:

        <?php
        preg_match_all( '`(\{search\})( .*)(\{/search\})`iU',$ string,$matches );
        $search=$matche s[0][0];
        $term=$matches[2][0];
        $string=str_rep lace($search,'< a href="somepage. php?keyword='.
        urlencode($term ).'">'.$term.' </a>',$string);
        ?>

        Also, it will take into account the formatting for the search term in
        the URL.

        --
        Justin Koivisto - spam@koivi.com
        PHP POSTERS: Please use comp.lang.php for PHP related questions,
        alt.php* groups are not recommended.
        SEO Competition League: http://seo.koivi.com/

        Comment

        • Jan Pieter Kunst

          #5
          Re: preg_replace and metatags...

          In article <c5gg2m$1c0vb$1 @ID-203069.news.uni-berlin.de>,
          Pedro Graca <hexkid@hotpop. com> wrote:
          [color=blue]
          > $txt1 = preg_replace('/(.*){search}(.* ){\/search}(.*)/i',
          > ## $1 $2 $3
          > '$1<a href="somepage. php?keyword=$2" >$2</a>$3', $txt0);[/color]


          I like that comment style! A very good idea with complicated
          preg_replaces. Thanks!

          JP

          --
          Sorry, <devnull@cauce. org> is een "spam trap".
          E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

          Comment

          • Pedro Graca

            #6
            Re: preg_replace and metatags...

            Jan Pieter Kunst wrote:[color=blue]
            > In article <c5gg2m$1c0vb$1 @ID-203069.news.uni-berlin.de>,
            > Pedro Graca <hexkid@hotpop. com> wrote:
            >[color=green]
            >> $txt1 = preg_replace('/(.*){search}(.* ){\/search}(.*)/i',
            >> ## $1 $2 $3
            >> '$1<a href="somepage. php?keyword=$2" >$2</a>$3', $txt0);[/color]
            >
            >
            > I like that comment style! A very good idea with complicated
            > preg_replaces. Thanks![/color]

            What about extended syntax? I like it even more that that one :)


            $txt1 = preg_replace(
            '/ # start of regular expression
            (.*) # = $1 in replacement string
            {search} # literal "search" enclosed in braces
            (.*) # = $2
            {\/search} # literal "{/search}"
            (.*) # = $3
            /ix', # end regexp, case insensitive, extended syntax

            '$1<a href="somepage. php?keyword=$2" >$2</a>$3', $txt0);

            --
            USENET would be a better place if everybody read: : mail address :
            http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
            http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
            http://www.expita.com/nomime.html : to 10K bytes :

            Comment

            Working...