Regular Expressions/ Cut String Problems<

liunx

Guest
Hi, I need 3 functions todo the following to a string:

1. cutting to a specific length
2. changing first letter of each line to a capital
3. and first letter of each word to a capital

Here's what I came up with but I'm having trouble with them.

1. mb_strcut // Is there an alternative because I don't have the mb things working on my php.
2. preg_replace("/\n(.)/e", strtoupper('\\1'), $string); // This just returned the same value
3. preg_replace("/\b(.)/e", strtoupper('\\1'), $string); // This returned an error

I didn't make the functions 2 and 3 I was told them by someone because I'm not too good at regular expressions. I'd be greatful if someone could help. Thanx1. substr() (<!-- m --><a class="postlink" href="http://uk.php.net/manual/en/function.substr.php">http://uk.php.net/manual/en/function.substr.php</a><!-- m -->)


string substr ( string string, int start [, int length])


substr() returns the portion of string specified by the start and length parameters.

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.



2. strtr() (<!-- m --><a class="postlink" href="http://uk.php.net/manual/en/function.strtr.php">http://uk.php.net/manual/en/function.strtr.php</a><!-- m -->)


string strtr ( string str, string from, string to)

string strtr ( string str, array replace_pairs)


This function returns a copy of str, translating all occurrences of each character in from to the corresponding character in to and returning the result.

If from and to are different lengths, the extra characters in the longer of the two are ignored.


example:


$trans = array(" a" => " A", " b" => " B", " c" => " C");

$test = strtr("this is a test");

$test[0] = strtoupp($test[0]);

echo $test;


This should result in:

This Is A TestOriginally posted by Horus_Kol
1. substr() (<!-- m --><a class="postlink" href="http://uk.php.net/manual/en/function.substr.php">http://uk.php.net/manual/en/function.substr.php</a><!-- m -->)




2. strtr() (<!-- m --><a class="postlink" href="http://uk.php.net/manual/en/function.strtr.php">http://uk.php.net/manual/en/function.strtr.php</a><!-- m -->)



example:


$trans = array(" a" => " A", " b" => " B", " c" => " C");

$test = strtr("this is a test");

$test[0] = strtoupp($test[0]);

echo $test;


This should result in:

This Is A Test

didn't you foreget the second paramater horus?


$trans = array(" a" => " A", " b" => " B", " c" => " C");

$test = strtr("this is a test",$trans);

$test[0] = strtoupp($test[0]);

echo $test;thanks willamoose - you spotted my deliberate mistake there ;)I tried what you said but I got array. I assumed that's because you put $test[0] so I changed all occurances of that to $test and then I just got a blank string. Also I noted what's is the function strtoupp. I escaped that line because I figured it may have been a mistake.

I'd be greatful if you could help. Thanxstrtoupp() forces a character to be uppercase.

I used it in the example to force the first character to upper case, as it has no space in front of it and therefore the strtr() function before it will ignore it.

It should work...it won't work. you spelled strtoupp wrong, and you don't need it.

strtoupper - Returns string with all alphabetic characters converted to uppercase

that is not what you wanted anyway. ;)

$test = explode(" ", "this is a test");
$countAll = count($test);
for ($i=0; $i<$countAll; $i++){
$string .= ucfirst($test[$i]);
$string .= " ";
}
echo $string;

that will work for 2 & 3, but #1 is easy

echo strlen($str); // will echo the number of characters.Cheers scoutt that worked for 3 but how would I change it for 2 (convert the first letter of each line to upppercase). Thanx again.if you have #3 then why worry about it?Doesn't matter, I got it working. I just changed " " to "\n" pretty simple really. Thanx for your help.then it won't work the way I had it. you will be missing the uppercase on every word.

if that is the case then what about new lines? \n will only cover a break but you also have to cover \r.
 
Back
Top