regular expression to underline a given word in a text...

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

    regular expression to underline a given word in a text...

    Hi,

    With the sentence :

    "Bordeaux est au bord de l'eau"

    How to do to underline, for instance, the word "eau" ? without underlining
    the substring of "Bordeaux" ?
    I don't know how to isolate the word...

    My current code :

    $text=eregi_rep lace("(".strips lashes($word_to _underline]).")","<b>\\0 </b>",$
    text);

    but this underline "eau" in "Bordeaux" too and i don't want to !

    Thanks for any help !

    Fred


  • uws

    #2
    Re: regular expression to underline a given word in a text...

    I <bfot13$b6v$2@n ews-reader2.wanadoo .fr>, Fred skrev:[color=blue]
    > How to do to underline, for instance, the word "eau" ? without underlining
    > the substring of "Bordeaux" ?
    > I don't know how to isolate the word...[/color]

    Try:

    preg_replace("/[^\w](eau)[^\w]/", "<u>\\1</u>", $text);
    [color=blue]
    > My current code :
    > $text=eregi_rep lace("(".strips lashes($word_to _underline]).")","<b>\\0 </b>",$text);[/color]

    Besides, use <u> for underlining text. <b> is for bold text.

    mvrgr, Wouter

    --
    :wq mail uws@xs4all.nl

    de welvaartstaat? :: of die bestaat? :: ze vraagt zich echt af -- monza

    Comment

    • Fred

      #3
      Re: regular expression to underline a given word in a text...

      Re,

      Thanks !
      it works perfectly !
      Some improvement :

      preg_replace("/([^\w])(".stripslashe s($tab_mots[$i]).")([^\w])/i",
      "\\1<b>\\2</b>\\3", $texte);

      (to correct a bug searching "l'eau" for instance)

      Now, how to find the position of this word in a text ?
      (not "eau" in "bordeaux" but the whole word "eau")

      Regards,

      fred

      "stephan beal" <stephan@wander inghorse.net> a écrit dans le message de
      news:bfp0us$ecr $1@ork.noris.ne t...[color=blue]
      > Fred wrote:[color=green]
      > > How to do to be case insensitive ?[color=darkred]
      > >> preg_replace("/[^\w](eau)[^\w]/i", "<u>\\1</u>", $text);[/color][/color]
      >
      > note the 'i' after the last / of the pattern.
      >
      > --
      > ----- stephan beal
      > Registered Linux User #71917 http://counter.li.org
      > I speak for myself, not my employer. Contents may
      > be hot. Slippery when wet. Reading disclaimers makes
      > you go blind. Writing them is worse. You have been Warned.
      >[/color]


      Comment

      • CC Zona

        #4
        Re: regular expression to underline a given word in a text...

        In article <bfot13$b6v$2@n ews-reader2.wanadoo .fr>,
        "Fred" <frederes@free. fr> wrote:
        [color=blue]
        > "Bordeaux est au bord de l'eau"
        >
        > How to do to underline, for instance, the word "eau" ? without underlining
        > the substring of "Bordeaux" ?
        > I don't know how to isolate the word...
        >
        > My current code :
        >
        > $text=eregi_rep lace("(".strips lashes($word_to _underline]).")","<b>\\0 </b>",$
        > text);
        >
        > but this underline "eau" in "Bordeaux" too and i don't want to ![/color]

        $unbolded=strip slashes($text);
        $bolded=preg_re place("/\b(eau)\b/", "<b>$1</b>", $unbolded);

        Note that matching will be case-sensitive.

        --
        CC

        Comment

        • uws

          #5
          Re: regular expression to underline a given word in a text...

          I <bfp3t1$coe$1@n ews-reader5.wanadoo .fr>, Fred skrev:[color=blue]
          > Now, how to find the position of this word in a text ?
          > (not "eau" in "bordeaux" but the whole word "eau")[/color]

          Look at strpos()

          mvrgr, Wouter

          --
          :wq mail uws@xs4all.nl

          and it's you i see :: but you don't see me -- coldplay

          Comment

          • CC Zona

            #6
            Re: regular expression to underline a given word in a text...

            In article <bfp0us$ecr$1@o rk.noris.net>,
            stephan beal <stephan@wander inghorse.net> wrote:
            [color=blue]
            > Fred wrote:[color=green]
            > > How to do to be case insensitive ?[color=darkred]
            > >> preg_replace("/[^\w](eau)[^\w]/i", "<u>\\1</u>", $text);[/color][/color]
            >
            > note the 'i' after the last / of the pattern.[/color]

            Note that doing so matches things like "eAu", "EaU", etc. but *only where
            some character not in the set [a-zA-Z0-0_] occurs both before and after*.
            If you also want to match against something like "Eau d'whatever", you need
            a better regex. Check out the solution I already offered using word
            borders \b. And if you want only the first letter to be case insensitive,
            substitute "/\b([eE]au)\b/" instead.

            --
            CC

            Comment

            • KAH

              #7
              Re: regular expression to underline a given word in a text...

              "Fred" <frederes@free. fr> wrote in
              news:bfot13$b6v $2@news-reader2.wanadoo .fr:
              [color=blue]
              > With the sentence :
              >
              > "Bordeaux est au bord de l'eau"
              ><<snipped>>[/color]

              Please do not multipost. If you feel the need for getting your post in
              multiple newsgroups, crosspost (add additional newsgroup names to the
              newsgroups: header in a comma-separated list). It saves the same solutions
              being repeated over and over in different groups, thus wasting the time of
              the people responding (as happened with the time I spent replying to you).

              KAH

              Comment

              Working...