Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Breaking Up A String Into Individual Words


I have a text box that I will use to search a database. I would like
to use it so that it will not use a whole string (ie. 'red striped
top') but instead break it up into individual words (ie. 'red',
'striped', 'top') and maybe put it into and array. I will then use the
words in the array to search the database.

How do I break a string up this way?

Also, is this the most common way of creating a means of searching a
database?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
How To Return A String With 5 Previous Words And 5 After Words
i have a database full of articles. I want to search that database with
keywords.
i expect this database to send me back a list of articles containing the
search string(s).
i would like to display the result like google does: the searchstring
highlighted within its context sentence (about 20 characters before, and 20
characters after the searchstring).

I'm wondering how to return the contextual sentence.

Breaking Up The Query String Dynamicly?
using $Get_string = $_SERVER['QUERY_STRING']; to receive a query string such as
month=may&$day=15&year=2006

so that $Get_string = "month=may&$day=15&year=2006"

can I separate the values and turn them into variables? (like a normally would with a url get)

$month = "may"
$day= "15" etc

it would be great to just do $_GET on the url but I'm sending the string as a variable to another script... so i can't just go the easy route.

Splitting Up Words In A String.
I have this form, the user fills it out it gets sent to the php page. The form text line gets stored in my variable called $variable.

now $variable may have 1, 2, 3, 4 or more words, seperated by spaces. Now how can I somehow split up each of those words and store them in an array or something. So say $variable = "Big giant cant say that word";

I want to put each of those words into a different array ellement.
so loop untill all of these words have been gotten.
$array[] = some how word #1 or 2 or whatever.

Replace Bad Words From A String
Want to replace bad words from a string.I have a list of bad words and allowed words in an array. 

For example,$allowedwords = array ("*inner", "*outer");
$badwords = array ("inner", "outer");

I want to remove the bad words and similar to bad words and allowed those allowed words similar to bad word.example string: This is an inner text in *inner with spinner.Output like : This is an **** text in *inner with sp****.

Parse A String For Certain Words
I was wondering if php can parse a text string for certain words and
return "true" if that word is found. For example, I have a string like this:

$string = "The rain in spain is the same as the rain on the plain";

I want to run a php command(s) that will parse the sentence and return true
if, for example, the word "spain" is found.

Replacing Words In A String
If I have a textfile of words, each word on one line:

dog
cat
viagra

for instance, and I get them into an array, how then can I replace those
words in a string with something else:

"The quick dog took viagra and the lazy cat was unhappy."

I want it to say:

"The quick ### took ### and the lazy ### was unhappy."

I've been banging my head against this, with foreach (both forms) and
rtrim, and preg_replace, and str_replace, and I can't get it to replace.

I don't need to see code, just a general "algorithm" would be useful.

Rrecognising Words From A String
someone submits a form with a selection of any of these words - to stop the whole service:

// stop
// cancel
// unsubscribe
// stoptips
// stoptip

1. I need to do one thing if these are found to be true in the string - firstly what function do I use for this? stristr($smsmsg, 'stop') this may not be quite enough for what follows.

2 .Secondly they can also stop a specific service by sending in stopservicename, this can take many forms but stop is always there - so my pridiciment from the first rule is that the word stop is there - and rule one would imply with my code.

3. Lastly they can send in the servicename to submit to the service

Again looking at 2 the servicename is also there so rule 2 would apply leaving me everything in tatters. So I need some REG exp I think to say if the words stop, cancel.. are alone or even part of a sentence without an adjoining word then go to 1.

Then I need for 2 if the word stop is followed directly with something then do 2 or go to 1, and that the adjoining word must be a keyword. Lastly if the word that relates to a keyword for subscription is alone then do 3. As you can see I am confused too and cant get my head around REG EXP so I need a little guidance in how someone would approach this.

Diff For Words In String
I'm comparing the text of (snippets of) web pages which I expect to be
quite different or quite similar. In the case where they are similar,
I would like to display the more recent one and say something like:
Word 2 added [before word 2 in original]: "Jack be nimble"

Words 10-11 changed to: "the quick brown fox"
[from words 9-11 in original]: "the brown fast quick fox"

Words [20-22 in original] before word 20 removed: "sat in a corner on"

One way to do this is to replace all spacing chars with
, write the
strings to two files, and then run diff (FC on my Win XP Pro = file
compare), collecting the output. Does anyone happen to have PHP code
for this where I don't have to write files? Note that the diff is on
words of the strings and not characters.

In particular, the normal algorithms for this (longest common
subsequence) only produce a number, and don't note the differences.

Also, I wanted to give a threshhold (about 10, say) and if the longest
common subsequence differs from the shorter string (strings are on the
order of 100 words) by at least this amount, then simply fail (since
the difference between the two strings would be deemed to great). This
should make the algorithm far more efficient. The corresponding
argument in FC would be /LB10.

Searching The Words In A String
I'm trying to disallow users from creating a username that has "admin" in it. I thought about using explode but after that I'm not sure about how to search the array and see if it's in there.

Count The Occurrence Of Words In A String
If you want to count the occurrence of words in a string, the
following routine may help you. For example if you supply the string
"hello hello Hello I am am Hasin" it will out put the followng

hello = 3
I = 1
am = 2
hasin = 1

Here is the code

<?php
$str = "hello hello Hello I am am hasin";
$word_count = (array_count_values(str_word_count(strtolower($str),1)));
ksort($word_count);
foreach ($word_count as $key=>$val)
echo $key ." = ". $val."<br/>";
?>

Which String() To Use To Display First 500 Words Of Data.
I have a table of large documents in mysql [text]. I want to use some sort of string() to display only about the first paragraph (or 500 words) to the user. (a preview). Then at the end have a ...(click here for more) link to the rest of the document. Which functions should I consider?

Counting Amount Of Characters Not Words In A String
I'm having trouble counting chars.

I can count the amount of words in a string with count() but not the amount of chars

I've checked all of the forums and php.net function list and havn't found anything usefull

Here's the code I'm currently working with, it counts words and not chars and that makes it inaccurate in resizeing a text box.


function wordcounter($text)
{
$astring = split(" ", $text);
$count = count($astring);
return $count;
}

$entrycount = wordcounter("$entry_description");
$rescount = wordcounter("$resolution_description");

if($entrycount > 40) {
$entryrows = 20;
}else{
$entryrows = 6;
}
if($rescount > 40) {
$resrows = 20;
}else{
$resrows = 6;
}

<textarea name='entry_description' WRAP='physical' cols=&#3950;' rows='<?PHP echo"$entryrows"; ?>'><?php echo $entry_description ?></textarea>

sombody please point this retard in the right direction.

Searching A Mysql Database For Words From A String ...
I am trying to allow users to search a mysql database by first name, last name, or subject heading. I have the search working if the people use only one word, but I can't get a string to work. If I implode the string to an array then iterate through the array I end up losing the benifits of the "group by" clause.

I have an article, subject, name, peoplelookup and subjectlookup table, and the select statement I am currently using is as follows:(sorry I don't know how to make this thing tab) PHP Code:

Convert A Long Sentence Into Words/string
how to convert a sentence into strings Eg. "I am just a beginner in this PHP" into new lines as follows:

I
am
just
a
beginner
in
this
PHP

Counting Words And Cut After Specific Total Words
This is my code to count words in a post: PHP Code:

Hit Counters For Individual Objects
I've been Googling for about an hour, and I must be using the wrong terminology because I'm only finding guides to overall site hit counters. I'm looking for some help devising hit counters for blog entries. I want to display (probably for myself) how many views, possibly unique views, and new comments using PHP and MySQL.

What should I be Googling for, or does anyone have some useful links? I'm not very knowledgeable about SESSIONS, so I could use some really good tutorials regarding them as well.

Explode Individual Characters
<?PHP
$var = 'hello everybody
$tar = explode('', $var);
foreach($tar as $key => $val)
{
if($val != 'e')
{
echo $val;
}
}
?>

this does not work. the error tells me that the explode delimeter is empty but this is what i want. im trying to explode the string into an array of its letters instead of just its words. is there another way of doing this then?

Summing Individual Rows In Mysql
I have created a php score sheet that allows graders to enter scores for different criteria. It uses a mysql database to store the information.

I want each row to total, and cannot figure out how to do that.

Since new people and new scores will be added to the list, the total row needs to total new people as well.

I'm pretty new to this, so don't assume I know syntax, etc.

Can anyone tell me how to do this?

Name Spelling Math Science Total
Joe 15 11 45
Mary 11 22 41
Sam 7 3 11

Replacing Individual Characters With Preg_replace
I found this code in a PERL script that was written by someone else:

$password =~ tr/a-zA-Z/n-za-mN-ZA-M/;

For example, "newuser1" translates to "arjhfre1" and vice versa. I
realize this isn't the best password encryption, but then I didn't
write the originating software either.

I am trying to convert this to it's PHP equivalent. I understand why
this works in PERL, but I can't figure out the syntax for the
preg_replace command to get it working in PHP. Can someone please
take a crack at it?

Trim() A Phrase Instead Of Individual Characters
I have a script that takes a URL and trims characters from it for storage in a database. My intention is to trim http:// from the start of the URL and and trailing slashes. For this I have been using the following:

$url = trim($url, "http://");

Unfortunately a problem has arisen in that any url's ending in .net have "t" trimmed from the end because, I realise, trim() strips out individual characters, not phrases.

Is there any way trim() can be modified to only find and remove the instance of http:// in it's entirety, and ignore single instances of the letters, or is there an alternative function I should be looking at?

Web Calendar Group/individual View Events?
There seem to be a lot of web calendars out there and I am having a tough time sorting out which ones to even try - a lot of the more popular ones were created 2 or 3 years ago.
Can anyone recommend a good one that uses php and mysql preferably?

One of the main uses would be for family to indicate their holidays so all in the family can see. Thus, it would require a group/public feature that allows users to see all "public" holidays or just their own holidays.

Create Image For Each Individual Character In A Truetype Font (.ttf)
Is there any possible way to create an individual image for each character in a TrueType font file (.ttf)? Eiether in PHP, or some Windows program that would do it?

Listing Table Data With An Option To Delete Each Individual Item.
Was wondering if anyone could help me with a PHP, MySQL problem. I am
completely new to PHP and MySQL. I have been trying to find a way to
list data from a table on a web page and after each individual item
have a hyperlink to allow users to delete the information listed. Can
anyone help?

i.e.
Name Age Sex
------------------------------------------
Adam 21 M delete
Betty 22 F delete
Chris 23 M delete
Daisy 24 F delete

------------------------------------------------------------------------------------------------------------------------------
(delete after 'Sex' would be the hyperlink to delete that entire line
(row) from the table).

Breaking Strings?
I have a string (actually 100's of these, but lets take one):

$string ="TI: Some journal title AU: SomeAuthor,-S; Smith,-A; Jones,-B SO: SomeSource. 2002 Jan 9; 287(2): 188; discussion 189-90 JN: Some Journal Title";

This needs to be turned into an array:

$entry = array(
'Some Item Title',
'SomeAuthor,-S; Smith,-A; Jones,-B',
'SomeSource. 2002 Jan 9; 287(2): 188; discussion 189-90',
'Some Journal Title');

As you can see, each array item should be defined by what comes after either/all of the following: 'TI:' 'AU:' 'SO:' 'JN:'

So, I need to be able to create a pattern that can look through the string and return everything in between TI: and AU:, then AU: and SO, etc.

for example:

eregi(???, $string, $entry);

I'm fairly new to this and can't seem to figure out the regex I need for the pattern. Is this even the way to go about it?

Breaking Out Of An Iframe
I cannot figure this one out. My script is being displayed in an iframe (a wiki page) and I have one option won't give me the correct results unless the page is displayed in either

a) new tab in NS browser
b) new browser instance.

The option in question is used to print forms, and I use the browser Print Preview for the page setup. It's all pretty useless if I have the page and iframe content in my preview.

I thought using header() would do the trick.

Breaking A Page Down.
What is the best solution to break down a php page into certain section using MySql, For example i'm looking at designing a page that reffers to a MySQL database that contains the html code for the navigation. So that in future if the navigation changes I'll only need to changed the html code in the database and all the pages will be sorted. I'm also looking at doing the same for the information on the footer of the page, with teh contact details.

Breaking Out Of Quotes
I know I can use stuff like in front of " so that it can't break out of a "" and start doing bad things, but it seems there are other situations where, E.G., if the string is hex for " then it has the same effect, and stuff like that, and it gets tricky escaping it all, so I'm wandering, is there some kind of option I can enable or extension I can install that just simply prevents objects/variables/arrays from breaking out of single/double quotes and also for mysql queries?

And also for html so E.G. echo"<img src='$asdf'>"; can't be exploited to do other things? I don't care if it isn't free as long as it works good and isn't to expensive..

Breaking Up Querys
I have a page that updates a table every workday. (these are standard updates, like if someone has moved, or has a different phone number or something.)  It is too big of a query to run the entire table all at once, so it is split out. 

The split is grouped by the first letter of a first name field.  Right now it is broken up into 5 parts, and each part has a group of letters (i.e. "a" to "d" is one group, "e" to "k" is the next, and so on.  As you can see, these do not have an even number of letters, but that is because some letters have a lot more entries, and need to be in smaller groups.) 

The reason it is split out into 5 groups is that I can run one group on each workday (Monday through Friday), and I can have the PHP check what day of the week it is.  (I use a case statement, the first case is for Monday, the second is for Tuesday, etc.) Code:

Breaking A Loop.
I have one going through rows of a sql database until it matches some data with user input. I need to put a break command in there but can't seem to find out how to achieve this.

Breaking Rows
I am using a while statement to pull results from my database. Now if I have like 40 results how would I make it break into rows.

So instead of;

result result result result result result result result result result result result result result result result result result

I want;

result result result result result
result result result result result
result result result result result
result result result result result

XML Parser Breaking Up URLs
I'm having a problem parsing an xml file in which one of an elements character data is a url which contains multiple "&" characters. For some reason the parser breaks up the data into multiple pieces. For instance, when I call this function(abbreviated):

function characterDataHandler ($parser, $data)
{
global $element;
global $url;
if ($element == "URL") {
$url = $data;
echo "url=".$url."<br>";
}
}

I get some output like this:


url=http://www.somewebsite.com/index.jsp?s=af

url=&

url=filter=1

url=&

url=show=all


Do urls with querystrings have to be handled differently then regular character data?

For Loops Breaking At A Certain Multiple
Can someone help me out with writing a script that will make will loop 500 times, but at every 4th multiple, it will insert a peice of code?

I tried all day to get this, and so far, nothing has worked.

Characters Breaking My Query
Im using encrypted data to store cc info. The PHP code encrypts and sends the result to the database. the problem is that the encrypted string contains lots of characters that will break my sql query. I can do a replace but there are lots of characters.

for example, a character will look like this:

g%$^'GDF$%^//"@#$%/||''$#"/|#dfsa.

how can I put this information in a different format... and then retrieve the data to decrypt the string?

Not Breaking Loop In Function
I get this error: <b>Fatal error</b>: Cannot break/continue 1 levels in <b>file.php</b> on line <b>128</b><br>

This happens when I call a function. In the function there is a break; This is cause the break is in a function and not in the loop. But it gets called inside the loop. Is there a fix for this?

E-Mail Not Breaking Lines
I'm sending an e-mail via a contact form, but somehow the lines aren't breaking in the text, it always looks like this:

lorem ipsum

dolor

sit

amet

lorem


Which is of course not readable at all. What am I doing wrong here? I'm using a few security checks and such for cleaning the field up; $text = trim($text);
...
$text = stripslashes(strip_tags($text));
...
$text = mysql_real_escape_string($text);
.....

#and then the part where it's included in the message:
$message .= "Nachricht: {$text} ";

Can any of those be causing the display problem?

Script Breaking On Mysql_result
I was trying to figure out why one of my scripts was failing and by using an "or die" statement for the SQL narrowed it down to this bit of code:

$md5 = md5_file("swf/" . basename($jpg_link));
$dupe = mysql_result(mysql_query("select count(gId) from games where gSwfFile='$md5.swf"), 0);
if(!empty($dupe))
{
continue;
}

This is giving the following error - "Warning: mysql_result(): supplied argument is not a valid MySQL result resource"

The thing is, I've checked that SQL query by running other values in place of $md5 through it and it appears to be fine. 

select count(gId) from games where gSwfFile='Flat War july 30th 2007.swf';
Any ideas?

Header Function - Breaking Out Of A Frame
I can create a plain htm link and have the new page break out of frames. a href='page.html' target='_top' Can this be done with a Header redirect in php?

Breaking Backwards Compatibility - Good Or Bad?
If you have any PHP scripts which will not work in the current releases
due to breaks in backwards compatibility then take a look at
http://www.tonymarston.net/php-mysq...everything.html and see if
you agree with my opinion or not.

Breaking Survey Into Seperate Parts?
I have a very long survey form with 4 different sections. Is there anyway with php to split this form into 4 popup windows so the person can click on the section they wish to fill out and when they are done, go back to the host survey form and continue on? I know there must be a way to do it, i'm just at a loss as to how i would pass the variables off? Cookies perhaps?

Breaking Up A Comma Seperated List
i have a list seperated by commas: 1. 3. 5. 7. 9 actually is a varible

$members ="1, 3, 5, 7, 9"; what would be the best way to break that up to insert the values into a table in a loop.

Breaking Up Text For Multiple Pages
I was recently put in charge of heading up my company's website, and
while I have a lot of experience on the design side of things, I'm
still very new to the programming side. When I started, the website had
just gotten a revision, but the site was an utter mess, so I've been
trying to fix it up. As I've gone along, I've learned some aspects of
PHP, but my knowledge is still very limited.

Here is the problem I'm trying to fix. A backend is used to enter
information into our MySQL database, and then a page template pulls it
out to make the page. For our reviews, the PHP coding currently breaks
up the review's text so that it can be places across multiple pages.
The problem is, the way the text breaking was set up, the end of one
page and the beginning of the next are totally chaotic, with breaks
often coming mid-sentence.

Here is what I'd love: other coding automatically puts an HTML break at
the end of paragraphs, and then another in the space between one
paragraph and the next. I'd love to have the code search for when those
double breaks come up, and then break up the text after, say, every
sixth pair of HTML breaks. I'd also like the option to break the text
myself by putting in some sort of text code in the original text. At
this point, though, anything that can make the breaks more elegant
would be a huge improvement.

I really appreciate any help anybody can give me in this one. Please
remember that I know very little of PHP, and I'm not much of a
programmer anyhow, so things that you may take for granted that people
would understand about PHP, I might not know.

I tried to pull out what I thought was the current coding for the text
break. Here it is. I'd also like to get rid of the part1/part2 factor.
Before, they had the text breaking to help in fitting into the layout
around an image, but I've fixed that so I no longer need the break. So,
that portion no longer needs to be a factor.

if ($page == ""){
$page = 1;
}
$no_letters = strlen($Game_Full_Story);
$times = ($no_letters / 3800) +1;
$Game_Full_Story = wordwrap($Game_Full_Story,3800,"*#^");
$Game_Full_Story = explode ("*#^", $Game_Full_Story);
$pages = array();
for ($i=0; $i<$times ; $i++){
$part[$i] = $Game_Full_Story[$i];
if ($i != 0){
array_push($pages, $i);
}
}
$page = $page-1;
$story = $part[$page];
if (strlen($story) < 460){
$parts1 = $story;
}else{
$story = wordwrap($story,460,"*#^");
$story = explode ("*#^", $story);
$parts1 = $story[0];
$count = count($story);
$parts2="";
for ($i=1; $i<$count; $i++){
$parts2 .=$story[$i];
$parts2 .=" ";
}
}

Breaking The BACK Button (Refresh Problem)
I have a php page which serves up multiple pages based on how the user
interacts with it - there are links on the first page that will reload (from
the same php file) a new page with form fields and submit buttons, and when
a user posts from that new page (or cancels), then the same php file is
again loaded, detecting how the user responded and generating the
appropriate html for whatever should be none next. All very typical.

What I want to know is, is there a way so that once a user has posted from
some submit button from one page (we'll say he Cancel's from a page that had
a form Cancel button so he returns to the original non-form default page
generated from the php file), can it be made so if the user then "refreshes"
the new page (hits the Refresh or Reload browser button) he does NOT get the
browser's "The page cannot be refreshed without resending the information.
Click Retry to send the information again, or click Cancel to return to the
page that you were trying to view." dialog? In other words, what I'd like is
a way to have the browser think that there was no posting done (even though
there was) once I've generated this certain 'home' page with my php code, so
that user ReLoad's of the page won't get this warning.

This is probably related also to problems I've had with "breaking the BACK
button" on sequences of php pages: if the user BACK's up to a page that was
generated by a post, it may not even be possible to regenerate the original
page.

Return Data Breaking Input Field
My return data is: (I have nothing to do with how the og person choose to input this)

<font color="#800080">CASE</font>

and I am setting the value of a input text form field equal to that return

$input      = "<input type="text" name="queOpt". $i ."" value="". $this->_queArr[$i] ."" />";

im lost on how to fix this. addslashes doesnt make sense  - maybe it is the double quote thing

PHP- First 10 Words In A Field
I know this is a simple procedure but how do I extract only the first 10 (or 20) words in a field named- DESCRIPTION.

PHP Reserved Words
I took a look to the - PHP reserved words - and found that do is included on that list. But I've been using a $do variable for a long time. Can anyone explain to me how this is possible?

Extracting Words
I'd like to extract the first 5 words from a blurb of text. I can't find an easy way to do that. Example. If I were to do that for this message I'd extract "I'd like to extract the"

Removing Bad Words
Looking for suggestions on how to handle bad words that might
get passed in through $_GET['item'] variables.

My first thoughts included using str_replace() to strip out such
content, but then one ends up looking for characters that wrap
around the stripped characters and it ends up as a recursive
ordeal that fails to identify a poorly constructed $_GET['item']
variable (when someone hand-types the item into the line and
makes a simple typing error).

So the next thoughts involved employing a list of good words
and if any word in the $_GET['item'] list doesn't fall into the
list of good words, then an empty string gets returned.

Any suggestions on how to handle this?

Reserved Words
So I'm making drop down menus each with a MySQL-stored value selected. No probs (built from tips on other posts, etc. If you want it, just ask).

EXCEPT, I have created an array of US states using 2-letter abbreviations. Oregon is OR - which kills the function because OR is reserved.

I have tried backslashes and quotes to escape the word - no luck. The function does work because I've used ORE instead for now. How might I escape this so it will work using OR as an array value?

Wiki Words
I'd like to know
how it is possible to parse a text
in order to find words matching
keywords included in a database.

I.e. like in a wiki site:

http://en.wikipedia.org/wiki/Symbolic_logic

Everytime a keyword is found in the text
the page automatically inserts a link
to the keyword's page.


Copyright © 2005-08 www.BigResource.com, All rights reserved