preg_replace help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Ross

    preg_replace help

    I dont think I'll ever understand regular expressions ... I need to do th
    efollowing and I just don't know where to start:

    $haystack = "How much wood would a wood chuck chuck if a woodchuck could
    chuck wood?";
    $needles = ("wood","chuck" ,"wood chuck");


    I want to replace every needle with an anchor tag like this:

    <a href="def.html? word=wood">wood </a>
    <a href="def.html? word=chuck">chu ck</a>
    <a href="def.html? word=wood%20chu ck">wood chuck</a>

    How do I do this?!?


    Thanks
    Alex



  • .:DeVa:. \( \)

    #2
    Re: preg_replace help

    <?php

    $lines = array ('How much wood would a wood chuck chuck if a woodchuck could
    chuck wood?');

    $old = array('wood', 'chuck', 'wood chuck','','','' ,'','');
    $new = array('<a href="def.html? word=wood">wood </a>','<a
    href="def.html? word=chuck">chu ck</a>','<a
    href="def.html? word=wood%20chu ck">wood chuck</a>','','','','' ,'');

    foreach ($lines as $line_num => $line) {
    echo str_replace($ol d, $new, $line);
    }

    ?>

    Hope this helps you...

    --
    function .:DeVa:. ( )
    {
    $url=" www.podvodni-svijet.com ";
    $quote=" One day all thinking will be done by machines.. ";
    $fact=" Valar morghulis! ";
    echo "$url - $quote - $fact ";
    }
    --
    Remove all FAKEs when replying to mail !


    Alexander Ross natipka¹e:[color=blue]
    > I dont think I'll ever understand regular expressions ... I need to
    > do th efollowing and I just don't know where to start:
    >
    > $haystack = "How much wood would a wood chuck chuck if a woodchuck
    > could chuck wood?";
    > $needles = ("wood","chuck" ,"wood chuck");
    >
    >
    > I want to replace every needle with an anchor tag like this:
    >
    > <a href="def.html? word=wood">wood </a>
    > <a href="def.html? word=chuck">chu ck</a>
    > <a href="def.html? word=wood%20chu ck">wood chuck</a>
    >
    > How do I do this?!?
    >
    >
    > Thanks
    > Alex[/color]


    Comment

    • Alexander Ross

      #3
      Re: preg_replace help

      thanks for the reply, but this wouldn't work ....

      in the first iteration of the for loop, it would replace the "wood" in "wood
      chuck"; then in teh third iteration. "wood chuck" would not be found


      Any other thoughts?


      ".:DeVa:. ( )" <FAKEvedran@FAK Epodvodni-svijet.com> wrote in message
      news:c5gu23$qp8 $1@bagan.srce.h r...[color=blue]
      > <?php
      >
      > $lines = array ('How much wood would a wood chuck chuck if a woodchuck[/color]
      could[color=blue]
      > chuck wood?');
      >
      > $old = array('wood', 'chuck', 'wood chuck','','','' ,'','');
      > $new = array('<a href="def.html? word=wood">wood </a>','<a
      > href="def.html? word=chuck">chu ck</a>','<a
      > href="def.html? word=wood%20chu ck">wood chuck</a>','','','','' ,'');
      >
      > foreach ($lines as $line_num => $line) {
      > echo str_replace($ol d, $new, $line);
      > }
      >
      > ?>
      >
      > Hope this helps you...
      >
      > --
      > function .:DeVa:. ( )
      > {
      > $url=" www.podvodni-svijet.com ";
      > $quote=" One day all thinking will be done by machines.. ";
      > $fact=" Valar morghulis! ";
      > echo "$url - $quote - $fact ";
      > }
      > --
      > Remove all FAKEs when replying to mail !
      >
      >
      > Alexander Ross natipka¹e:[color=green]
      > > I dont think I'll ever understand regular expressions ... I need to
      > > do th efollowing and I just don't know where to start:
      > >
      > > $haystack = "How much wood would a wood chuck chuck if a woodchuck
      > > could chuck wood?";
      > > $needles = ("wood","chuck" ,"wood chuck");
      > >
      > >
      > > I want to replace every needle with an anchor tag like this:
      > >
      > > <a href="def.html? word=wood">wood </a>
      > > <a href="def.html? word=chuck">chu ck</a>
      > > <a href="def.html? word=wood%20chu ck">wood chuck</a>
      > >
      > > How do I do this?!?
      > >
      > >
      > > Thanks
      > > Alex[/color]
      >
      >[/color]


      Comment

      • .:DeVa:. \( \)

        #4
        Re: preg_replace help

        Alexander Ross natipka¹e:[color=blue]
        > thanks for the reply, but this wouldn't work ....
        >
        > in the first iteration of the for loop, it would replace the "wood"
        > in "wood chuck"; then in teh third iteration. "wood chuck" would not
        > be found
        >
        >
        > Any other thoughts?[/color]

        Your right, didnt think about that, hmm... dont nowm I'am in hurry ill try
        to solve that later...

        Good Luck..

        --
        function .:DeVa:. ( )
        {
        $url=" www.podvodni-svijet.com ";
        $quote=" One day all thinking will be done by machines.. ";
        $fact=" Valar morghulis! ";
        echo "$url - $quote - $fact ";
        }
        --
        Remove all FAKEs when replying to mail !



        Comment

        • Justin Koivisto

          #5
          Re: preg_replace help

          Alexander Ross wrote:
          [color=blue]
          > I dont think I'll ever understand regular expressions ... I need to do th
          > efollowing and I just don't know where to start:
          >
          > $haystack = "How much wood would a wood chuck chuck if a woodchuck could
          > chuck wood?";
          > $needles = ("wood","chuck" ,"wood chuck");
          >
          >
          > I want to replace every needle with an anchor tag like this:
          >
          > <a href="def.html? word=wood">wood </a>
          > <a href="def.html? word=chuck">chu ck</a>
          > <a href="def.html? word=wood%20chu ck">wood chuck</a>[/color]

          Very similar to the "preg_repla ce and metatags..." thread:

          <?php
          $string='How much wood would a wood chuck chuck if a wood'."\n".'chu ck
          could chuck wood?';
          $search = array('wood\s*c huck','wood','c huck');
          $pattern='`('.j oin('|',$search ).')`iUs';

          preg_match_all( $pattern,$strin g,$matches);
          $search=$matche s[0][0];
          $term=preg_repl ace('/(\r\n|\r|\n)/',' ',$matches[1][0]);
          $string=str_rep lace($search,'< a href="def.html? keyword='.
          urlencode($term ).'">'.$term.' </a>',$string);
          ?>

          --
          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

          Working...