Capitalizing using explode or preg_replace_callback

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
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Capitalizing using explode or preg_replace_callback

Post by swetha »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


how do i write a function for capitalizing the first letter of a sentence using
explode or preg_replace_callback?

have written this:

Code: Select all

<?php
$split=explode(".",$str);
for($i=0; $i<count($split); $i++) 
{
   $temp[$i]=ucfirst(trim($split[$i])); 
}
$output = implode(".",$temp);
return $output;
?>
but there is a slight problem
it trims spaces in output and does not use capitalise letters in sentences after ? or !


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Why am i getting this output?

Post by swetha »

Also,
how do i write a code for capitalizing the first letter of a sentence using
preg_replace_callback or preg_match?
how r u?
i am fine
should output as
How r u?
I am fine


or
i am here.
where r you?
should output as
I am here.
Where r u?



Note that the input is in 2 sentences.
The first character if alphabet should be a capslock.If the line ends by a "?" or ".",the next line will start with a capital letter.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Why am i getting this output?

Post by Ziq »

Not necessarily use explode() or prer_match() function. Like example:

Code: Select all

 
<?
$data = '
hello!
hi.
how r u?
fine.
ppodasd...
dsfwerewrwr.
';
 
$length = strlen($data);
$new_sentence = true;
for ($i = 0; $i < $length; $i++)
{   
    if ($data{$i} == '.' || $data{$i} == '!' || $data{$i} == '?') { $new_sentence = true; continue; }
    
    if (!$new_sentence) continue;
    
    if ($new_sentence)
    {
        if (trim($data{$i}))
        {
            $data{$i} = strtoupper($data{$i});
            $new_sentence = false;
        }
    }
}
 
echo $data;
?>
 
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Why am i getting this output?

Post by Ziq »

P.S. New question better ask in new topic. IMHO
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Capitalizing using explode or preg_replace_callback

Post by pickle »

Split topic.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Capitalizing using explode or preg_replace_callback

Post by swetha »

this will only capitalise strtoupper($data{$i}),where data($i) is !,. or ?
also what does the following statement check for ?
if trim($data($i))
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Capitalizing using explode or preg_replace_callback

Post by swetha »

Code: Select all

 
<?php
$split=explode(".",$str);
for($i=0; $i<count($split); $i++) 
{
   $temp[$i]=ucfirst(trim($split[$i])); 
}
$output = implode(".",$temp);
return $output;
?>
 

can u pls modify the above code to capitalise the firt letter of a sentence i.e the function should do the following:
input:
how r u?
i am fine.
we are here!
how is life?

ouput :
How r u?
I am fine.
We are here!
How is life?

also am trying to do the above using preg_replace_callback.could you pls help as to how i need to modify this code
specially the $pattern to accomodate new lines i.e "\n"?

Code: Select all

 
$str="How r u?I am fine.We are here!How is life?";
$pattern="/[!.?]\w/";
function process($mat)
{
   return strtoupper($mat[0]);
}
$output=preg_replace_callback($pattern,'process',$str);
echo $output;
 
note:in the actual case,$str will have the string in separate lines as shown in the input.

hope im clear.i basically need to capitalise the first letter of a sentence using one of the above codes with the required
modifications.thanks for help.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Capitalizing using explode or preg_replace_callback

Post by Ziq »

Sorry, I'm not understand. What is bad in my snippet? Why need use

Code: Select all

 
<?php
$split=explode(".",$str);
for($i=0; $i<count($split); $i++)
{
   $temp[$i]=ucfirst(trim($split[$i]));
}
$output = implode(".",$temp);
return $output;
?>
 
Last edited by Ziq on Sun Sep 14, 2008 2:35 am, edited 2 times in total.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Capitalizing using explode or preg_replace_callback

Post by swetha »

what does this post mean
deleted?
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Capitalizing using explode or preg_replace_callback

Post by swetha »

also..pls help in answering this?

this will only capitalise strtoupper($data{$i}),where data($i) is !,. or ?
also what does the following statement check for ?
if trim($data($i))
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Capitalizing using explode or preg_replace_callback

Post by Ziq »

Code: Select all

<?
$data = '
hello!
hi.
how r u?
fine.
ppodasd...
dsfwerewrwr.
';
 
$length = strlen($data);
$new_sentence = true;
for ($i = 0; $i < $length; $i++)
{       
    //  if we have symbol '.' or '!' or '?' it means that it new sentence 
    if ($data{$i} == '.' || $data{$i} == '!' || $data{$i} == '?') { $new_sentence = true; continue; }
   
    //  It's for optimizing speed. Needn't to look at each symbol, if it's no first.
    if (!$new_sentence) continue;
   
    //  If it's a first symbol after .!? (new_sentence == true!)
    if ($new_sentence)
    {
        //  If this symbol isn't whitespace
        if (trim($data{$i}))
        {
            //  capitalize this symbol
            $data{$i} = strtoupper($data{$i});
            //  It's no new sentence now
            $new_sentence = false;
        }
    }
}
 
echo $data;
?>
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Capitalizing using explode or preg_replace_callback

Post by swetha »

Thanks,this fucntion works.But i wanted to implement the same using preg_match
and preg_replace...the coding would require only few lines.
Post Reply