473,415 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,415 software developers and data experts.

Search Result help

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
8 2175
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
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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Greg | last post by:
I have a page that searches a database by a repairman's name and by a date range. It pulls info by the repairman's name but pulls all info in the database regardless of the date. Below is the code...
0
by: Hriday | last post by:
Hi there, I am working on a web application in ASP.NET My web server and AD machine are in the same domain but located on diffrent phisical machine, I am not able to search user's info by the...
3
by: ZafT | last post by:
Thanks for any help in advance. I am using this exact same piece of code on a server running PHP4, but for some reason when I run it on dreamhost using PHP5, the search does not register and it...
3
by: Sheau Wei | last post by:
This is the search engine code that i create, but it was error and didnt come out the result. Cn u help me to check what wrong with my code? Thanks <Table cellspacing=1 cellPadding=1...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
10
by: jonathan184 | last post by:
Hi I tried getting this to work through dreamweaver but it did not. So i found a n example on the internet , i followed everything exactly the search script does not work. Could somebody help me...
4
by: bendlam | last post by:
I have a page that contains search criteria and when you click on the search button it causes a post back that populates a dataview on the same page. One of the gridview columns contains a link...
3
by: Bigalan | last post by:
Hello, i am relatively new to PHP and i am struggling with printing multiple search results on to different pages. The code below works ok but when you click on next page button, it brings up a blank...
5
by: nicehulk | last post by:
I'm doing a library for DVD-movies and I'm trying to figure out how to search different categories like director, title, actor etc. (which are all variables in a DVD-class). Previously I did like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.