multiple string matching | Member | | Join Date: Sep 2007
Posts: 49
| | |
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
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | 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: -
$_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="background-color: $ffcc00;">';
-
}
-
-
$html .= $str[$_i];
-
-
if( ! empty($_end[$_i]) )
-
{
-
$html .= '</span>';
-
}
-
}
-
-
echo $html;
-
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
| | | 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). -
<?php
-
$_start = array(5 => true, 12 => true);
-
-
$_end = array(10 => true, 18 => true);
-
-
$str='ABCDREGYRIWJEKSALOPRHDAGRTPRTDBRTWASERFSDHSJHDS';
-
-
-
$html = '';
-
-
for( $_i = 0; isset($str[$_i]); ++$i )
-
-
{
-
-
if( ! empty($_start[$_i]) )
-
-
{
-
-
$html .= '<span style="background-color: $ffcc00;">';
-
-
}
-
-
-
-
$html .= $str[$_i];
-
-
-
-
if( ! empty($_end[$_i]) )
-
-
{
-
-
$html .= '</span>';
-
-
}
-
-
}
-
-
-
-
echo $html;
-
?>
-
thanks
kumar
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: multiple string matching
Heya, Kumar.
Please use CODE tags when posting source code:
[CODE=php]
PHP code goes here.
[/CODE]
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | 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
| | | 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
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: multiple string matching
Heya, Kumar.
Yes. It looks like I made another typo. -
$html .= '<span style="background-color: $ffcc00;">';
-
Should be: -
$html .= '<span style="background-color: #ffcc00;">';
-
| | Member | | Join Date: Sep 2007
Posts: 49
| | | 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
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|