Im having a problem using trim function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesha
Forum Newbie
Posts: 11
Joined: Tue May 01, 2007 3:15 am

Im having a problem using trim function

Post by icesha »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hello. i really need a help regarding the usage of trim function in my code. i want to remove the "." in the end of my sentence. but the real problem is when the period is connected with the word for example : "this is a test."
My purpose if one of the words is spelled incorrectly(using pspell)analyse if it wrong.
My output would be test is spelled incorrectly because of the period at the end, which is wrong since test is spelled correctly.
this is my code. please see why does the trim function doesnt work. I need help. please.
and regarding an english grammar check. will someone give me some samples of code.
thank you very much.

Code: Select all

//explodes into sentences
//explodes into words
//checks spelling ang tags the word.


<?php
include("ecsel_connectdb.php");


$var= trim($_POST['textfield']);
$mypar=explode('.',$var);
foreach($mypar as $b)
{
echo "$b - This is a sentence";
echo "<br/>";
echo "<br/>";


}

$myarray=explode(' ',$var);
foreach($myarray as $a)
{
$pspell_link = pspell_new("en");
if (pspell_check($pspell_link, $a))
{
echo "$a - This is a valid spelling";
echo "<br/>";
echo "<br/>";

}
else
{

echo "$a - Sorry, wrong spelling";
echo "<br/>";
echo "<br/>";

$pspell_link = pspell_new("en");
if (!pspell_check($pspell_link, $a))
{
$suggestions = pspell_suggest($pspell_link,$a);
foreach ($suggestions as $suggestion) {
echo "Possible spelling: $suggestion<br />";
echo "<br/>";
echo "<br/>";

}
}
echo "<br/>";


echo " word: $a";
echo "<br/>";
echo "<br/>";

$sql="SELECT WORD FROM WORDS where WORD= '$a'";
$sql_result=mysql_query($sql,$connection) or die ("Couldnt execute query at WORDS");
$row= mysql_fetch_array($sql_result);


if($a==$row["WORD"])
{

$sql1= "SELECT MARK FROM WORDS where word ='$a'";
$sql_result1= mysql_query($sql1,$connection) or die ("Couldnt execute query at words");
$row1=mysql_fetch_array($sql_result1);


$dis = $row1["MARK"];
echo "$a is $dis";
echo "<br/>";
echo "<br/>";

}

else
{
echo "$a - noun";
echo "<br/>";
echo "<br/>";
}

}
}

$string= implode(' ',$myarray);
echo "string: $string";
echo "<br/>";

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

trim() takes an optional character list. Read the manual for details.

As for "and regarding an english grammar check. will someone give me some samples of code." ... that's not going to happen. Grammar checking is way too complicated to implement in form validation.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You can pass a charlist as second parameter to trim(), e.g.

Code: Select all

<?php
$s = '...    xyz .  . . . .     ';
echo '-', trim($s, "\t\r\n ."), '-';
Post Reply