Page 1 of 1

Capitalizing using explode or preg_replace_callback

Posted: Fri Sep 12, 2008 2:35 am
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.

Re: Why am i getting this output?

Posted: Fri Sep 12, 2008 5:15 am
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.

Re: Why am i getting this output?

Posted: Fri Sep 12, 2008 8:15 am
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;
?>
 

Re: Why am i getting this output?

Posted: Fri Sep 12, 2008 8:19 am
by Ziq
P.S. New question better ask in new topic. IMHO

Re: Capitalizing using explode or preg_replace_callback

Posted: Fri Sep 12, 2008 9:57 am
by pickle
Split topic.

Re: Capitalizing using explode or preg_replace_callback

Posted: Sat Sep 13, 2008 6:28 am
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))

Re: Capitalizing using explode or preg_replace_callback

Posted: Sat Sep 13, 2008 7:18 am
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.

Re: Capitalizing using explode or preg_replace_callback

Posted: Sun Sep 14, 2008 2:19 am
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;
?>
 

Re: Capitalizing using explode or preg_replace_callback

Posted: Sun Sep 14, 2008 2:26 am
by swetha
what does this post mean
deleted?

Re: Capitalizing using explode or preg_replace_callback

Posted: Sun Sep 14, 2008 2:27 am
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))

Re: Capitalizing using explode or preg_replace_callback

Posted: Sun Sep 14, 2008 2:42 am
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;
?>

Re: Capitalizing using explode or preg_replace_callback

Posted: Mon Sep 15, 2008 12:32 am
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.