Jump to content

Remove then add punctuation from string


The Little Guy

Recommended Posts

I am using pspell, and when a user enters some text and submits it, I check the words, but I need to remove the punctuation first so I have this:

 

$str = preg_replace('/\W[ ]/', ' ', $_GET['q']);

 

I then create an array from the spaces and check each word like so:

 

$words = explode(" ", $str);
$pspell_link = pspell_new("en");
$errors = 0;
$errorStr = "";
foreach($words as $word){
if(!pspell_check($pspell_link, $word)){
	$suggestions = pspell_suggest($pspell_link, $word);
	$errorStr .= "<b>".$suggestions[0]."</b> ";
	$errors++;
}else{
	$errorStr .= $word." ";
}
}

 

So, now I would like to display the string, with the errors, in bold (like in the code), but how do I put the punctuation back?

Link to comment
https://forums.phpfreaks.com/topic/185563-remove-then-add-punctuation-from-string/
Share on other sites

Put $str = preg_replace('/\W[ ]/', ' ', $_GET['q']);

inside of the foreach and execute it individually on each word.

But use it like so:

$checkWord=preg_replace('/\W[ /', ' ', $word);

 

Then do your spell check on "$checkWord".

Now, if it needs modified, modify the actual array value.

-Change your foreach to foreach($words as $key=>$word){

 

and replace the word with: $words[$key]="<b>{$word}</b>";

 

Then at the end, use "implode" to put your string back together.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.