PDA

View Full Version : search term


cgibie
02-19-2006, 10:24 AM
I tried to do a search feature, I want to bold out that specific term after search.. the code is :


while (/content: (.*)\n/ig) {
@content = "$1";
}

foreach $content (@content) {
if ($content =~ /$search/i) {
if ( /content: (.*)\n/ig ) {
print "$1<p>\n";
}
}

}


I have no idea where to bold it since it wil print $1, any thoughts?

FishMonger
02-19-2006, 02:33 PM
Why are you applying this regex twice? /content: (.*)\n/ig

Is this what you're looking for?
print "<b>$1</b><p>\n";

cgibie
02-19-2006, 05:19 PM
Fishmonger, what happened was that in my text file, there is a "content" section (I read it as a record).... so I want the users to search the "specific" word in that content section, that's why I loop that @content array, however, you see I want to print out the content to the page too. It's like if you put a specific term, the code will search and give you the result by displaying its content. My problem is I don't have a clue to blod that specific term within that content after the success search.

FishMonger
02-19-2006, 06:42 PM
I would need to see a sample of your input data and your expected output to be sure, but from what you describe, you should be able to drop the foreach loops and use a substitution regex in the while loop.

Here's an example
#!/usr/bin/perl -w

$search = 'dream';

while(<DATA>) {
s/\b($search)\b/<b>$1<\/b>/ig;
print "$_<p>\n";
}

__DATA__
I say to you today, my friends, so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream.

I have a dream that one day this nation will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident: that all men are created equal."

I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood.

I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice.

I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character.

I have a dream today.

Here's the html code it outputsI say to you today, my friends, so even though we face the difficulties of today and tomorrow, I still have a <b>dream</b>. It is a <b>dream</b> deeply rooted in the American <b>dream</b>.
<p>

<p>
I have a <b>dream</b> that one day this nation will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident: that all men are created equal."
<p>

<p>
I have a <b>dream</b> that one day on the red hills of Georgia the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood.
<p>

<p>
I have a <b>dream</b> that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice.
<p>

<p>
I have a <b>dream</b> that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character.
<p>

<p>
I have a <b>dream</b> today.
<p>

cgibie
02-19-2006, 08:37 PM
Fishmonger, you are awsome man. No wonder why it is Monger, hehe.... but that $_ prints everything out include the part that I dont want to display..


File: blog.jpg
Title: Blog
Content: This is blah blah blah


The searched term got bolded out. But it prints File:blog.jpgTitle:BlogContent:This is blah blah...

I don't want it to print that File:blog.jpg, I want it to print only that Title and Content but the entries in that text file are read as a record....

FishMonger
02-19-2006, 08:52 PM
There are several ways to accomplish this, but it might be easier and better if you posted a little larger sample of your code and the record data so I can run a few tests.

cgibie
02-19-2006, 08:59 PM
Here it is



open(IN, "$dir/$file") or &dienice ("Could not open the $file: $!");
#flock(IN, LOCK_SET);
#seek(IN, 0, SEEK_SET);

# $/ holds each record after \n in the text file
# double quote not single quote
local $/ = "\n\n";
@lines = reverse<IN>;

foreach (@lines) {
while (/entry: (.*)\n/ig ) {
$myword1 = "$1";
}

while(/category: (.*)\n/ig) {
$cat = "$1";
}

while (/content: (.*)\n/ig) {
@content = "$1";
}

# loop through the array content to get the words in that array
foreach $content (@content) {

# if Entry is equal to $category print the entry out
if ($content =~ /$search/i) {
print "<table id=entry><td valign=middle width=500 bgcolor=0066CC>";
print "<b>Entry#: " . $myword1;
print "</td></table>";
print "<br>";

while ( /((date|title): .*)\n/ig) {
print "<b>$1</b><br>\n";
}

# print the content out with the specific term bolded and red
#if (/content: (.*)\n/ig) {
s/\b($search)\b/<b><font color=red>$1<\/b><\/font>/ig;
print "$_<p>\n";
#}

if (/url: (.*)\n/ig) {
$url = "$1";
}

if ( /file: (.*)\n/ig ) {
print "<p>";
print "<a href=$photourl/$1 target=_blank><img src=$photourl/$1 border=0 width=200 height=220></a><p>\n";
}



Pretend there are closing loops

FishMonger
02-19-2006, 10:21 PM
Please post the first (or last) 3 entries in your file so that I can run a realistic test without having to make up data like I did on that first example.

cgibie
02-19-2006, 11:00 PM
Date: Sun Feb 19 00:02:31 2006
Entry: 10
Category: Love
Month: February
Day: 18
Year: 2006
Title: Test
Content: Test the blog
File1: C:\Documents and Settings\Owner\Desktop\Shirt\01010001.JPG
file: 01010001.JPG


Date: Sun Feb 19 00:08:01 2006
Entry: 11
Category: computer
Month: February
Day: 18
Year: 2006
Title: Test
Content: I got up and went to school today
File1:




Like this?

FishMonger
02-19-2006, 11:56 PM
This may need a little tweeking, but I think it's close to what you want.
#!/usr/bin/perl -w

$search = 'school';

{
local $/ = "\n\n";
@record = reverse <DATA>;
}

foreach (@record) {
next unless /:/;
@lines = (split /\n/)[0..7];
foreach (@lines) {
($key, $value) = split(': ', $_, 2);
$blog_entry{$key} = $value;
}

if ( $blog_entry{'Content'} =~ s/\b($search)\b/<b><font color=red>$1<\/b><\/font>/ig ) {
print "<table id=entry><td valign=middle width=500 bgcolor=0066CC>\n",
"<b>Entry#: $blog_entry{'Entry'}</td></table><br>\n",
"<b>$blog_entry{'Date'}</b><br>\n",
"<b>$blog_entry{'Title'}</b><br>\n",
"$blog_entry{'Content'}<p>\n";
}
}

__DATA__
Date: Sun Feb 19 00:02:31 2006
Entry: 10
Category: Love
Month: February
Day: 18
Year: 2006
Title: Test
Content: Test the blog
File1: C:\Documents and Settings\Owner\Desktop\Shirt\01010001.JPG
file: 01010001.JPG

Date: Sun Feb 19 00:08:01 2006
Entry: 11
Category: computer
Month: February
Day: 18
Year: 2006
Title: Test
Content: I got up and went to school today
File1:

Here's its output:
<table id=entry><td valign=middle width=500 bgcolor=0066CC>
<b>Entry#: 11</td></table><br>
<b>Sun Feb 19 00:08:01 2006</b><br>
<b>Test</b><br>
I got up and went to <b><font color=red>school</b></font> today<p>

cgibie
02-20-2006, 12:21 AM
Wow you rock, man! It works now....