multiple string matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    multiple string matching

    Hi All,
    I have a string of around 500 characters(alph abets) and I have a 3 arrays storing the position "start, end and type" for that string. i am trying to do is to match these position to the string and highlight those positions on the web page.

    For example: string- ABCDREGYRIWJEKS ALOPRHDAGRTPRTD BRTWASERFSDHSJH DS

    start_pos=5
    end_pos=10
    type=helix breaker

    start_pos=12
    end_pos=18
    type=unusual

    so what I am trying to do is to match the pos and type to the string and
    in the output(webpage) , trying to highlight the positions, may be drawing a rectangular box around the matched positions.and positions may overlap within each other.

    I am tying to use some of the functions for the string matching but unable to do for multiple matches.



    any help or suggestions.

    Thanks
    Kumar
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Kumar.

    The only way to do this reliably would be to output HTML to set the background color of the characters.

    My recommendation would be to compile all your start indexes into an array and your end indexes into a separate array. Then loop through your string and output it progressively:
    [code=php]
    $_start = array(5 => true, 12 => true);
    $_end = array(10 => true, 18 => true);

    $html = '';
    for( $_i = 0; isset($str[$_i]); ++$i )
    {
    if( ! empty($_start[$_i]) )
    {
    $html .= '<span style="backgrou nd-color: $ffcc00;">';
    }

    $html .= $str[$_i];

    if( ! empty($_end[$_i]) )
    {
    $html .= '</span>';
    }
    }

    echo $html;
    [/code]

    This code assumes that there is an end index for every start index and that no ranges overlap each other.

    Comment

    • kumarboston
      New Member
      • Sep 2007
      • 55

      #3
      Hi,
      Thanks for the response, I tried tho run the code with a test string, it showed me error of memory size limit. I am posting the code(php).
      [code=php]
      <?php
      $_start = array(5 => true, 12 => true);

      $_end = array(10 => true, 18 => true);

      $str='ABCDREGYR IWJEKSALOPRHDAG RTPRTDBRTWASERF SDHSJHDS';


      $html = '';

      for( $_i = 0; isset($str[$_i]); ++$i )

      {

      if( ! empty($_start[$_i]) )

      {

      $html .= '<span style="backgrou nd-color: $ffcc00;">';

      }



      $html .= $str[$_i];



      if( ! empty($_end[$_i]) )

      {

      $html .= '</span>';

      }

      }



      echo $html;
      ?>
      [/code]
      thanks
      kumar
      Last edited by pbmods; Sep 16 '07, 06:29 PM. Reason: Added CODE tags.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Kumar.

        Please use CODE tags when posting source code:

        &#91;CODE=ph p]
        PHP code goes here.
        &#91;/CODE]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Kumar.

          A 'memory limit exceeded' error generally means an infinite loop, and sure enough, I put '++$i' in my for loop instead of '++$_i'.

          Comment

          • kumarboston
            New Member
            • Sep 2007
            • 55

            #6
            Thanks for the help and resposnse.
            I ran the code again with [++$_i], its simply printing the $str string.

            Am i missing something??

            thanks once again for your time and patience.

            kumar

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Kumar.

              Yes. It looks like I made another typo.
              [code=php]
              $html .= '<span style="backgrou nd-color: $ffcc00;">';
              [/code]

              Should be:
              [code=php]
              $html .= '<span style="backgrou nd-color: #ffcc00;">';
              [/code]

              Comment

              • kumarboston
                New Member
                • Sep 2007
                • 55

                #8
                Thanks so much
                the code is working and its highlighting the start and the end points.
                I was wondering if i want to print the position exactly below the start match and the end match , then should i have to specify the column.
                because right now i am printing the position which is not below the matched charecters.

                kumar

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, Kumar.

                  You'll want to enclose your characters inside of <pre> tags. You can create an additional variable to hold the positional data; simply output a space character if the current position is not special, or else $_i if it is.

                  It gets a bit tricky when you get into double digits, but not impossible.

                  Comment

                  Working...