simple grep question

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

    #1

    simple grep question

    I want to do the following:

    replace __abc__ with <b>abc</bin a string

    Any help?

    Ben

  • Edward Z. Yang

    #2
    Re: simple grep question

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Ben wrote:
    I want to do the following:
    >
    replace __abc__ with <b>abc</bin a string
    Don't use grep. Try this (untested):

    preg_replace('/__([a-z0-9]+)__/i', '<b>$1</b>', $string);

    You may want to expand the character class.

    - --
    Edward Z. Yang GnuPG: 0x869C48DA
    HTML Purifier <htmlpurifier.o rg Anti-XSS HTML Filter
    [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFGVhZWqTO +fYacSNoRAoRwAJ 9sFagIahwNchBKq gY8RgPn7qb+fQCf bouO
    Yfq3Z6Mii++oowl 1GL8ItyE=
    =LqGd
    -----END PGP SIGNATURE-----

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: simple grep question

      At Thu, 24 May 2007 15:26:54 -0700, Ben let his monkeys type:
      I want to do the following:
      >
      replace __abc__ with <b>abc</bin a string
      >
      Any help?
      >
      Ben
      Don't use regex if not necessary.

      $fixedstring = str_replace ('__abc__','<b> abc</b>',$yourstring ,[$count]);
      $count, if supplied, tallies the number of replacements made.

      HTH
      Sh.

      Comment

      • gosha bine

        #4
        Re: simple grep question

        On 25.05.2007 00:26 Ben wrote:
        I want to do the following:
        >
        replace __abc__ with <b>abc</bin a string
        >
        Any help?
        >
        Ben
        >
        preg_replace('/__(\w+?)__/', '<b>$1</b>', $string)

        or

        preg_replace('/\b__(\w+?)__\b/', '<b>$1</b>', $string)

        the latter replaces only whole words, i.e. "xxx __blah__ xxx" but not
        "zzz__blah___zz z".



        --
        gosha bine

        extended php parser ~ http://code.google.com/p/pihipi
        blok ~ http://www.tagarga.com/blok

        Comment

        • Toby A Inkster

          #5
          Re: simple grep question

          Ben wrote:
          replace __abc__ with <b>abc</bin a string
          Assuming this is for a simplified text markup system, don't reinvent the
          wheel.

          Take a look at:

          - Markdown
          - Textile
          - BB Code
          - Wiki markup

          There are freely available PHP implementations of each of these. Some are
          in PEAR -- you'll need to use Google for the others.

          --
          Toby A Inkster BSc (Hons) ARCS
          [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
          [OS: Linux 2.6.12-12mdksmp, up 90 days, 22:48.]

          Non-Intuitive Surnames

          Comment

          Working...