By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Search Result help

P: n/a
I am trying to do the following with my search script that looks for records
in a mysql table. The following is an example of what I am trying to do.

Text being searched:
--
The brown fox jumped over the green fence then jumped into the web monitor.
It was hurt so it jumped backwards and fell on its!
--

The word we're searching for "web".

The results should look like

--
...then jumped into the *web* monitor. It was...
--

Are there any functions out there that will do this? Or what would it take
to make one?

--
Sharif T. Karim
....you don't know wrath yet...
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Sharif T. Karim wrote:
I am trying to do the following with my search script that looks for records
in a mysql table. The following is an example of what I am trying to do.

Text being searched:


The brown fox jumped over the green fence then jumped into the web
monitor. It was hurt so it jumped backwards and fell on its!
Return all records and use something like:

database query:

select field from table where field like "% web %";
// in this case the space before will get you web and not webster etc...
$pattern = "web";
$string = [string returned from mysql]
$replacement = "<b>*$pattern*</b>"
echo preg_replace($pattern, $replacement, $string);

Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #2

P: n/a
Michael Austin, being the foo Michael Austin is, wrote:
Sharif T. Karim wrote:
I am trying to do the following with my search script that looks for
records in a mysql table. The following is an example of what I am
trying to do.

Text being searched:


The brown fox jumped over the green fence then jumped into the web
monitor. It was hurt so it jumped backwards and fell on its!
Return all records and use something like:

database query:

select field from table where field like "% web %";
// in this case the space before will get you web and not webster
etc...
$pattern = "web";
$string = [string returned from mysql]
$replacement = "<b>*$pattern*</b>"
echo preg_replace($pattern, $replacement, $string);


Thanks, but that only replaces words. I am trying to get the result
($string) in this case, to start showing the part where the first occurence
of $pattern is.

--
Sharif T. Karim
....you don't know wrath yet...
Jul 17 '05 #3

P: n/a
In article <gO******************@twister.nyc.rr.com>, Sharif T. Karim wrote:
I am trying to do the following with my search script that looks for records
in a mysql table. The following is an example of what I am trying to do.

Text being searched:
--
The brown fox jumped over the green fence then jumped into the web monitor.
It was hurt so it jumped backwards and fell on its!
--

The word we're searching for "web".


I'm pretty sure you'll find an answer to this in a newsgroup about mysql
or in the mysql manual.

In case mysql doesn't have such a function, you could always use the '%
$var %' query and then use strpos en substr to get the desired result.
--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #4

P: n/a
Sharif T. Karim wrote:
Are there any functions out there that will do this? Or what would it take
to make one?


str_replace(); preg_replace()
strpos()
substr()
strlen()
Check them out at the fine manual

http://www.php.net/<function>

eg
http://www.php.net/str_replace

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #5

P: n/a
Sharif T. Karim wrote:
Michael Austin, being the foo Michael Austin is, wrote:
Sharif T. Karim wrote:

I am trying to do the following with my search script that looks for
records in a mysql table. The following is an example of what I am
trying to do.

Text being searched:


The brown fox jumped over the green fence then jumped into the web
monitor. It was hurt so it jumped backwards and fell on its!
Return all records and use something like:

database query:

select field from table where field like "% web %";
// in this case the space before will get you web and not webster
etc...
$pattern = "web";
$string = [string returned from mysql]
$replacement = "<b>*$pattern*</b>"
echo preg_replace($pattern, $replacement, $string);

Thanks, but that only replaces words. I am trying to get the result
($string) in this case, to start showing the part where the first occurence
of $pattern is.


I can't always give you the exact syntax. what I gave you was close, but
you need to be able to take what you are given and test/modify it to
suit your needs...

$search-from-form = $_POST['inputfield'];
$pattern = "-$search-from-form-";
//sear pattern needs non-alphanumberic delimiters
$string = "The web is a scary place to be";
//string to be searched
$rpat = "$search-from-form";
//now replace the string with the search "word"
$replacement = "<b>*$rpat*</b>";
echo "STRING = $string<br>\n";
echo preg_replace($pattern, $replacement, $string);

results in
The *web* is a scary place to be
(only in your browser *web* is bold).

This took a whole 10 minutes to prepare... is this for your job or just
playing/learning? Did you happen to read the docs on the previous
example? If not, why not???
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #6

P: n/a
Tim Van Wassenhove wrote:
In article <gO******************@twister.nyc.rr.com>, Sharif T. Karim wrote:
I am trying to do the following with my search script that looks for records
in a mysql table. The following is an example of what I am trying to do.

Text being searched:
--
The brown fox jumped over the green fence then jumped into the web monitor.
It was hurt so it jumped backwards and fell on its!
--

The word we're searching for "web".

I'm pretty sure you'll find an answer to this in a newsgroup about mysql
or in the mysql manual.

In case mysql doesn't have such a function, you could always use the '%
$var %' query and then use strpos en substr to get the desired result.

unfortunately it looks like another "programmer" that shouldn't be as
he/she was already given the answer, he/she just couldn't figure out how
to use it... No it wasn't perfect, but you should at least read the docs
- and if you can't find them, the above statement really does apply...
And companies actually hire people like this??
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #7

P: n/a
"Sharif T. Karim" <sh****@nyc.rr.com> wrote in message
news:gO******************@twister.nyc.rr.com...
I am trying to do the following with my search script that looks for records in a mysql table. The following is an example of what I am trying to do.

Text being searched:
--
The brown fox jumped over the green fence then jumped into the web monitor. It was hurt so it jumped backwards and fell on its!
--

The word we're searching for "web".

The results should look like

--
..then jumped into the *web* monitor. It was...
--

Are there any functions out there that will do this? Or what would it take
to make one?

--
Sharif T. Karim
...you don't know wrath yet...


Hey, think I got the winning entry!

<?

$text = 'The brown fox jumped over the green fence then jumped into
the web monitor. It was hurt so it jumped backwards and fell on its!';

$word = 'web';

$pattern = '/\b(.{0,24})\b('.preg_quote($word).')\b(.{0,16}\S)\ b/si';

if(preg_match($pattern, $text, $matches)) {
extract($matches, EXTR_PREFIX_ALL, 'm');
echo "..$m_1*$m_2*$m_3...";
}

?>

Do I get a prize?
--
Obey the Clown - http://www.conradish.net/bobo/
Jul 17 '05 #8

P: n/a
Michael Austin, being the foo Michael Austin is, wrote:
Sharif T. Karim wrote:
Michael Austin, being the foo Michael Austin is, wrote:
Sharif T. Karim wrote:
I am trying to do the following with my search script that looks
for records in a mysql table. The following is an example of what
I am trying to do.

Text being searched:

The brown fox jumped over the green fence then jumped into the web
monitor. It was hurt so it jumped backwards and fell on its!
Return all records and use something like:

database query:

select field from table where field like "% web %";
// in this case the space before will get you web and not webster
etc...
$pattern = "web";
$string = [string returned from mysql]
$replacement = "<b>*$pattern*</b>"
echo preg_replace($pattern, $replacement, $string);

Thanks, but that only replaces words. I am trying to get the result
($string) in this case, to start showing the part where the first
occurence of $pattern is.


I can't always give you the exact syntax. what I gave you was close,
but you need to be able to take what you are given and test/modify
it to suit your needs...

$search-from-form = $_POST['inputfield'];
$pattern = "-$search-from-form-";
//sear pattern needs non-alphanumberic delimiters
$string = "The web is a scary place to be";
//string to be searched
$rpat = "$search-from-form";
//now replace the string with the search "word"
$replacement = "<b>*$rpat*</b>";
echo "STRING = $string<br>\n";
echo preg_replace($pattern, $replacement, $string);

results in
The *web* is a scary place to be
(only in your browser *web* is bold).

This took a whole 10 minutes to prepare... is this for your job or
just playing/learning? Did you happen to read the docs on the previous
example? If not, why not???
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)


You obviously are such a genius that you completely missed the point to the
question. Part of it was to highlight keywords, that part I know how to do.
The other part, the one you keep looking past, is how to display only the
part of the result where the first occurrence of the keyword is.

Say the text var being searched:

$text = 'PHP - The best programming language on the face of the planet. The
manual is at php dot net. It is ... awesome!';

The keywords in the search
$words = 'manual';

I'd like the result to display as:

....The *manual* is at php dot...

Do you get me now? Look at your code, it does NOT do that. It simply bolds
the keywords.

Take it easy guy...
--
Sharif T. Karim
....you don't know wrath yet...
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.