Page 1 of 2

Forcing sentence start with Capital letter

Posted: Tue Jun 17, 2008 6:47 pm
by koyauni
I am making a auto conversion from Arabic script into Latin. Users will be able to cut and past Arabic based text into text area and click convert. I have made it work accept for one crucial thing. Since in Arabic based alphabet there is no distinguish between small and capital letters I am having problem to force the converted sentences starting with capital letters. I appreciate any help in this regard.
Project is based at
http://kurdan.org/converter/arablatin.php

An example such as
(... ئه‌سته‌مترین ئه‌گه‌ره‌کانی. فه‌رهه‌نگی) => will become (... estemtirín egerekaní. ferhengí)

I want that the word ferhengí or any word after (.) starts with a capital letter in this case . Ferhangí

Thanks

Re: Forcing sentence start with Capital letter

Posted: Tue Jun 17, 2008 7:17 pm
by Zoxive

Re: Forcing sentence start with Capital letter

Posted: Tue Jun 17, 2008 7:42 pm
by John Cartwright

Code: Select all

$data = ucfirst(preg_replace_callback('#\.\s([a-z])#i', create_function('$matches', 'return strtoupper($matches[0]);'), $data));

Re: Forcing sentence start with Capital letter

Posted: Tue Jun 17, 2008 7:47 pm
by helraizer
If you need just the first word capital , use

ucfirst("estemtirín egerekaní. ferhengí") = "Estemtirín egerekaní. ferhengí"

If you want every word to start with capital you should use

ucwords("estemtirín egerekaní. ferhengí") = "Estemtirín Egerekaní. Ferhengí"

EDIT: If you want sentences to start with a capital letter.

Something like:

Code: Select all

 
$sentence = explode(".", "estemtirín egerekaní. ferhengí");
 
//sentence[0] = estemtirín egerekaní
//sentence[1] = ferhengí
 
$output = implode(". ", ucfirst($sentence));
 
//$output = Estemtirín egerekaní. Ferhengí 
 
 
That should work.

Sam

Re: Forcing sentence start with Capital letter

Posted: Tue Jun 17, 2008 7:49 pm
by John Cartwright
helraizer wrote:If you need just the first word capital , use

ucfirst("estemtirín egerekaní. ferhengí") = "Estemtirín egerekaní. ferhengí"

If you want every word to start with capital you should use

ucwords("estemtirín egerekaní. ferhengí") = "Estemtirín Egerekaní. Ferhengí"
Neither of those fit his requirement. He needs the first letter of the first word after a period to be capatalized.

Re: Forcing sentence start with Capital letter

Posted: Tue Jun 17, 2008 8:04 pm
by helraizer
Jcart wrote:
helraizer wrote:If you need just the first word capital , use

ucfirst("estemtirín egerekaní. ferhengí") = "Estemtirín egerekaní. ferhengí"

If you want every word to start with capital you should use

ucwords("estemtirín egerekaní. ferhengí") = "Estemtirín Egerekaní. Ferhengí"
Neither of those fit his requirement. He needs the first letter of the first word after a period to be capatalized.
Yeah, I realised that, that's why I added the EDIT. :)

Re: Forcing sentence start with Capital letter

Posted: Tue Jun 17, 2008 8:18 pm
by John Cartwright
helraizer wrote:

Code: Select all

 
$sentence = explode(".", "estemtirín egerekaní. ferhengí");
 
//sentence[0] = estemtirín egerekaní
//sentence[1] = ferhengí
 
$output = implode(". ", ucfirst($sentence));
 
//$output = Estemtirín egerekaní. Ferhengí 
 
 
You cannot pass an array to ucfirst, therefore you'll need to apply a callback to the array.. which is almost exactly what I demonstrated.

Re: Forcing sentence start with Capital letter

Posted: Wed Jun 18, 2008 7:17 am
by koyauni
Thank you guys,
The sentence I posted here was just an example. I want every intial letter after last full stop (.) start a Capital letter.

This is just a sentence I am posting here. Capital letter should appear here.

Re: Forcing sentence start with Capital letter

Posted: Wed Jun 18, 2008 8:20 am
by helraizer
koyauni wrote:Thank you guys,
The sentence I posted here was just an example. I want every intial letter after last full stop (.) start a Capital letter.

This is just a sentence I am posting here. Capital letter should appear here.
Jcart's first code will do just that.

Re: Forcing sentence start with Capital letter

Posted: Wed Jun 18, 2008 8:42 am
by koyauni
Thanks,

I did that but there is no action,
I have created a file aphabet.php where I do all convention and in anther file I display the results as

$ArabicText = $_POST['ArabicText'];

# Substitutions
include ("alphabet.php");
$StageOne = strtr($ArabicText, $alphabet);
$StageTwo = strtr($StageOne, $alphabet2);
$StageThree = strtr($StageTwo, $alphabet3);
$StageFour = strtr ($StageThree, $alphabet4);

Where should I add this function to be included? Do I have to define the type $data?

Re: Forcing sentence start with Capital letter

Posted: Sun Jun 22, 2008 11:12 am
by koyauni
I manged to do this by

create array as

'. a' => '. A',

for every single letter of the alphabet. I know is not pragmatically smart way to do but it is working.

One issue which has make it hard for me is that in conversion process where in the initial text there is st sign " a \ is implemented and think this is the default PHP way of seeing the sign " ,
Is there any way to stop PHP doing that insertion?

A test like

Arabic text Arabic text Arabic text "Arabic text" Arabic text Arabic text.

after conversation become

Latin text Latin text Latin text \"Latin text\" Latin text Latin text.

I do not need the back slash \ in converted text.

Re: Forcing sentence start with Capital letter

Posted: Mon Jun 23, 2008 2:35 am
by helraizer
koyauni wrote:I manged to do this by

create array as

'. a' => '. A',

for every single letter of the alphabet. I know is not pragmatically smart way to do but it is working.

One issue which has make it hard for me is that in conversion process where in the initial text there is st sign " a \ is implemented and think this is the default PHP way of seeing the sign " ,
Is there any way to stop PHP doing that insertion?

A test like

Arabic text Arabic text Arabic text "Arabic text" Arabic text Arabic text.

after conversation become

Latin text Latin text Latin text \"Latin text\" Latin text Latin text.

I do not need the back slash \ in converted text.
That's PHP protecting itself. Or you used mysql_real_escape_string or addslashes. If you had a text field and wrote "> then you could inject HTML and script into the site (for that time, not perminant). Use:

Code: Select all

 
$arabic_text = stripslashes($arabic_text);
 
Stripslashes will strip these slashes and give you just the " instead of \"

Re: Forcing sentence start with Capital letter

Posted: Mon Jun 23, 2008 2:46 pm
by koyauni
Thank you it worked prefect.

I have solved the problem with the capital letter at the start of sentence. Now what if the world is the start of a paragraph? How do make the first letter of the paragraph a capital letter?

Re: Forcing sentence start with Capital letter

Posted: Thu Jun 26, 2008 6:59 pm
by koyauni
I found answer at
http://uk3.php.net/ucfirst

Another way to capitalize first letter of every sentence in a text, I hope it will help someone. It won't convert non-English characters, though, and ignores sentences ending with ! or ? etc.

<?php

$text="this is a sentence. this is another sentence.";

$split=explode(". ", $text);
foreach ($split as $sentence) {
$sentencegood=ucfirst($sentence);
$text=str_replace($sentence, $sentencegood, $text);
}

echo $text; // This is a sentence. This is another sentence.

?>

Re: Forcing sentence start with Capital letter

Posted: Sun Jun 29, 2008 5:55 am
by koyauni
I have a php.ini on my server with - register_globals = Off which appernetly do not allow for POST variable to work as

<textarea ><? print ($_POST["ArabicText"]) ?></textarea> // value in the form is picked up by this

if(isset($_POST[ArabicText])) { // called up here
$ArabicText = stripslashes($ArabicText);


Any idea how I can work around this without setting Gregister ON which makes Drupal 6.2 unstable?