Jump to content

How do i detect a capital letter in a sentence


receiver19

Recommended Posts


<?php 

$string = $_GET['string']; 

$count = strlen($string); 

$i = 0; 
$ii = 0; 

while($i < $count) 
{ 
        $char = $string{$i}; 
        if(ereg("[A-Z]", $char, $val)){ 
                $ii++; 
                $strings[$ii] .= $char; 
        } else { 
                $strings[$ii] .= $char; 
        } 
$i++; 
} 

var_dump($strings); 

?>

Link to comment
Share on other sites

Thanks for all the replied.

 

<?php
$str = 'GetToKnowYou';
$new_str = rtrim(preg_replace('#([A-Z][^A-Z]*)#', '$1 ', $str));
var_dump($new_str);

 

This works very well! However, i don't get it on this part ('#([A-Z][^A-Z]*)#', '$1 ', . I still don't understand though i have read the php manual. Anyone mind to explain to me?

Link to comment
Share on other sites

The preg_replace() function searches through a string matching the whole thing or parts of it to a regular expression pattern.

 

In this case the the pattern '#([A-Z][^A-Z]*)#' means match a string starting with one capital letter followed by any number of characters not matching a capital letter.

 

#: a delimiter marking the start of the actual regular expression pattern

(: start of a capture group

[A-Z]: a character class matching one character in the range A-Z

[^A-Z]*: a character class matching one character not in the range A-Z (that could be a, b, c, 12, #, [ ect.) and actually any number of them (including zero) because of the modifier *

): end of capture group

#: a delimiter marking the end of the actual regular expression pattern

 

Now the syntax of the preg_replace() is: preg_replace($regular_expression, $what_to_replace_with, $string_to_search_and_replace). The search is done with the previous mentioned pattern and we are using '$1 ' as the replacement. $1 means the first capture group or sub pattern - what's inside the parentheses - it's called a back reference. So each time the regex engine matches the previously discussed pattern it assign it's value to $1 and '$1 ' of course adds a space.

 

So going through this string 'GetToKnowYou' will first match 'Get' ([A-Z] followed by two [^A-Z]) and assign it's value to $1. So 'Get' is replaced with 'Get '. Now it matches 'To' and replaces it with 'To ' and so on. So in the end it's 'Get To Know You'.

 

Read more on regular expression here: http://www.regular-expressions.info/, http://www.regular-expressions.info/tutorial.html, http://www.regular-expressions.info/reference.html

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.