Forcing sentence start with Capital letter

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

koyauni
Forum Newbie
Posts: 8
Joined: Tue Jun 17, 2008 6:39 pm

Forcing sentence start with Capital letter

Post 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
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Forcing sentence start with Capital letter

Post by Zoxive »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Forcing sentence start with Capital letter

Post by John Cartwright »

Code: Select all

$data = ucfirst(preg_replace_callback('#\.\s([a-z])#i', create_function('$matches', 'return strtoupper($matches[0]);'), $data));
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Forcing sentence start with Capital letter

Post 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
Last edited by helraizer on Tue Jun 17, 2008 8:02 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Forcing sentence start with Capital letter

Post 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.
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Forcing sentence start with Capital letter

Post 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. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Forcing sentence start with Capital letter

Post 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.
koyauni
Forum Newbie
Posts: 8
Joined: Tue Jun 17, 2008 6:39 pm

Re: Forcing sentence start with Capital letter

Post 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.
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Forcing sentence start with Capital letter

Post 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.
koyauni
Forum Newbie
Posts: 8
Joined: Tue Jun 17, 2008 6:39 pm

Re: Forcing sentence start with Capital letter

Post 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?
koyauni
Forum Newbie
Posts: 8
Joined: Tue Jun 17, 2008 6:39 pm

Re: Forcing sentence start with Capital letter

Post 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.
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Forcing sentence start with Capital letter

Post 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 \"
koyauni
Forum Newbie
Posts: 8
Joined: Tue Jun 17, 2008 6:39 pm

Re: Forcing sentence start with Capital letter

Post 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?
koyauni
Forum Newbie
Posts: 8
Joined: Tue Jun 17, 2008 6:39 pm

Re: Forcing sentence start with Capital letter

Post 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.

?>
koyauni
Forum Newbie
Posts: 8
Joined: Tue Jun 17, 2008 6:39 pm

Re: Forcing sentence start with Capital letter

Post 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?
Post Reply