Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2003
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to remove "comma, full stop" from a sentence

    hello,

    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.

  • #2
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #3
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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=array(0,0,0);
    $overall=0;
    for($i=0;$i<3;$i++){
    $quote[$i]=str_replace(",","",$quote[$i]);
    $quote[$i]=str_replace(".","",$quote[$i]);
    $count[$i]=sub_str(" to "," ".strtolower($quote[$i])." "); //appends a single space to the sting, converts it to lowercase...
    $overall+=$count[$i];
    }

    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)
    Last edited by nicklim; 10-20-2003 at 09:20 AM.

  • #4
    New Coder
    Join Date
    Feb 2003
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    New Coder
    Join Date
    Feb 2003
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •