View Full Version : how to remove "comma, full stop" from a sentence

10-20-2003, 08:49 AM

i have sentences as follow:

$quote[1] = "To be or not to be, that is the question.";
$quote[2] = "Love looks not with the eyes, but with the mind.";
$quote[3] = "Love all, trust a few. Do wrong to none.";

now I would like to do the following,

1) for each quote[$i], remove all ","(comma) or "."(full stop)...does anyone know how? I have the following code but it does not work. What should I write in $patterns?

for ($i=1; $i<4; $i++)
$quote[$i] = strtolower($quote[$i]);
echo "$quote[$i]<BR>";
$pieces = explode(" ", $quote[$i]);
for ($j=0; $j< (count($pieces)); $j++)
echo "$pieces[$j] ";
$patterns = "\.\";
$replacement = "";
print preg_replace($patterns, $replacement, $string);
echo "$pieces[$j] <BR>";

2) also, I would like to count a) number of frequecy of each term (e.g. "to" has a frequency of 2 in qoute[1]) and b) number of frequency for all three quotes (i.e. "to" has frequency = 3)

but I have no idea how? can anyone please give me an idea?

Thank you very very much.

10-20-2003, 09:09 AM
i am guessing.. str_replace(msg,',',''); (or something like that.. i don't have the manual handy with me now... you can check it out)

(i used it to replace \n's with <br>'s)

10-20-2003, 09:55 AM
sub_str(" to ",msg); should return the count of the occurance of to...
(i added a space infront to prevent it from counting the words containing the word to. (you may need to modify it to take it if it's the beginning of the sentance or end of the sentance)

the way i will do the whole thing is by...

(assuming $quote is the string to change...)

$count[$i]=sub_str(" to "," ".strtolower($quote[$i])." "); //appends a single space to the sting, converts it to lowercase...

not exactly the most efficient and beautiful code, but it should get the job done.. [untested!!!] but it should work...
(sorry for posting twice.. forgot about the edit button earlier)

10-20-2003, 11:03 AM
thanks you very much...

One more question, if all the three sentences are read from another file, for example:

quote.txt has the following;

To be or not to be, that is the question.
Love looks not with the eyes, but with the mind.
Love all, trust a few. Do wrong to none.

1) how do I read this file?
2) how do I do the same as I wanted to do before?

Thank you very much for your kind help

10-20-2003, 07:06 PM
Is this a homework assignment? Because it sure sounds like one.

You should attempt to write the code and then ask specific questions if you run into problems. Don't just ask people to write the code for you.

As for you questions. To open a file in PHP you can use the function fopen(). It description and use can be found in the manual at www.php.net

10-21-2003, 11:53 AM
thank you