Regular Expressions

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

    Regular Expressions

    How do I change only the first occurence of a token in a string?

    How do I change only the nth occurence of a token in a string?

    How do I change the first alpha character after the nth occurence of a
    token in a string?


    The answer to these questions will allow me to display raw text as
    beautiful magazine style layouts using style sheets without manually
    altering each document.

  • Marek Möhling

    #2
    Re: Regular Expressions

    > How do I change only the first occurence of a token in a string?[color=blue]
    > How do I change only the nth occurence of a token in a string?
    > How do I change the first alpha character after the nth occurence of a
    > token in a string?[/color]

    These functions, either alone (preg-match, less convenient: preg_replace)
    or in combination with substr will do.

    Find details here:




    And, please check for the meaning of RTFM.
    (one of them is: read the f......riendly manual)


    Marek

    bn548mm@g214mx. net
    (remove numbers to despam)


    Comment

    • Kevin

      #3
      Re: Regular Expressions

      On 8 Feb 2005 09:40:27 -0800
      "petrovitch " <webmaster@delt afarms.com> wrote:
      [color=blue]
      > How do I change only the first occurence of a token in a
      > string?[/color]

      Well, only the first occurence is simpler than it seems.

      $string = 'lalalalalalala lal';
      $p = strpos($string, 'a');

      if ($p === false)
      exit;

      $string{$p} = 'R'; /* replacement letter */

      print $string;
      [color=blue]
      > How do I change only the nth occurence of a token in a string?[/color]

      You don't want only the last, right? You want to pass a value
      like 5, so you can change the 5th occurrence of a token, right?

      I think you can use the same approach above. Consider this
      function here. There can be bugs here, but hopefully you get the
      idea.

      function getpos($s,$c,$n ) {

      $i = $k = 0;
      for ( ;; ) {
      $_p=strpos($s, $c, $i);
      if( $_p === false) break;

      $i = $_p + 1; ++$k; $p = $_p;
      }

      if( $k < $n ) return -1;

      return $p;
      }

      And then you'd use this way:

      $p = getpos('sometim es', 'e', 2);
      $string{$p} = 'R'; print $string;

      That is, you want the position of the letter 'e' at its second
      occurrence. If there isn't a second occurrence, then -1 is
      returned.
      [color=blue]
      > How do I change the first alpha character after the nth
      > occurence of a token in a string?[/color]

      Well, with the function above you get the position of the token,
      then you search the original string for the first alpha char
      starting from the return of getpos(). It shouldn't be hard, I'll
      leave that to you.

      In C, there are macros/functions like isspace(), isalpha(), so
      you can increment your array index (string index, however PHP
      calls it) and at the first time you get an alpha, you get the
      position from your index/counter...

      Look for something like in PHP or write a function to do it.
      Write back if you need further help.
      [color=blue]
      > The answer to these questions will allow me to display raw text
      > as beautiful magazine style layouts using style sheets without
      > manually altering each document.[/color]

      I think I realize one problem with my suggestions. What you
      called token, I assumed it was a single character... if you need
      to look for strings, you will need to adapt the code.

      I hope it helps, though.

      Comment

      • Stian Berger

        #4
        Re: Regular Expressions

        On 8 Feb 2005 09:40:27 -0800, petrovitch <webmaster@delt afarms.com> wrote:
        [color=blue]
        > How do I change only the first occurence of a token in a string?
        >
        > How do I change only the nth occurence of a token in a string?
        >
        > How do I change the first alpha character after the nth occurence of a
        > token in a string?
        >
        >
        > The answer to these questions will allow me to display raw text as
        > beautiful magazine style layouts using style sheets without manually
        > altering each document.
        >[/color]

        Change first occurance:
        preg_replace("/test/","cake",$text, 1);
        Use the limit parameter to do just one replace.

        Change the 3rd occurance + next character. It isn't nice, but works.
        preg_replace("/((?:.*?test.*?) {2})test(\s*)(\ w)(.*)/s","\\1cake\\2< span>\\3</span>\\4",$text );
        Passes two occurances, to replace the third.
        Puts span tags around the next alpha char.

        Hope this helps.

        --
        Stian

        Comment

        Working...