Connecting Tech Pros Worldwide Help | Site Map

multiple string matching

Member
 
Join Date: Sep 2007
Posts: 49
#1: Sep 16 '07
Hi All,
I have a string of around 500 characters(alphabets) 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- ABCDREGYRIWJEKSALOPRHDAGRTPRTDBRTWASERFSDHSJHDS

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's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Sep 16 '07

re: multiple string matching


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:
Expand|Select|Wrap|Line Numbers
  1. $_start = array(5 => true, 12 => true);
  2. $_end = array(10 => true, 18 => true);
  3.  
  4. $html = '';
  5. for( $_i = 0; isset($str[$_i]); ++$i )
  6. {
  7.     if( ! empty($_start[$_i]) )
  8.     {
  9.         $html .= '<span style="background-color: $ffcc00;">';
  10.     }
  11.  
  12.     $html .= $str[$_i];
  13.  
  14.     if( ! empty($_end[$_i]) )
  15.     {
  16.         $html .= '</span>';
  17.     }
  18. }
  19.  
  20. echo $html;
  21.  
This code assumes that there is an end index for every start index and that no ranges overlap each other.
Member
 
Join Date: Sep 2007
Posts: 49
#3: Sep 16 '07

re: multiple string matching


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).
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.       $_start = array(5 => true, 12 => true);
  3.  
  4.       $_end = array(10 => true, 18 => true);
  5.  
  6.  $str='ABCDREGYRIWJEKSALOPRHDAGRTPRTDBRTWASERFSDHSJHDS';
  7.  
  8.  
  9.       $html = '';
  10.  
  11.       for( $_i = 0; isset($str[$_i]); ++$i )
  12.  
  13.       {
  14.  
  15.           if( ! empty($_start[$_i]) )
  16.  
  17.          {
  18.  
  19.               $html .= '<span style="background-color: $ffcc00;">';
  20.  
  21.           }
  22.  
  23.  
  24.  
  25.           $html .= $str[$_i];
  26.  
  27.  
  28.  
  29.           if( ! empty($_end[$_i]) )
  30.  
  31.           {
  32.  
  33.               $html .= '</span>';
  34.  
  35.           }
  36.  
  37.       }
  38.  
  39.  
  40.  
  41.       echo $html;
  42. ?>
  43.  
thanks
kumar
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Sep 16 '07

re: multiple string matching


Heya, Kumar.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Sep 16 '07

re: multiple string matching


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'.
Member
 
Join Date: Sep 2007
Posts: 49
#6: Sep 16 '07

re: multiple string matching


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
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#7: Sep 16 '07

re: multiple string matching


Heya, Kumar.

Yes. It looks like I made another typo.
Expand|Select|Wrap|Line Numbers
  1. $html .= '<span style="background-color: $ffcc00;">';
  2.  
Should be:
Expand|Select|Wrap|Line Numbers
  1. $html .= '<span style="background-color: #ffcc00;">';
  2.  
Member
 
Join Date: Sep 2007
Posts: 49
#8: Sep 16 '07

re: multiple string matching


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
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#9: Sep 17 '07

re: multiple string matching


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