/    Sign up×
Community /Pin to ProfileBookmark

Using this function, How can I get it so that it will uppercase the first letter after a parentheses, (a cat) would become (A Cat), and not (a Cat).

to post a comment
PHP

38 Comments(s)

Copy linkTweet thisAlerts:
@chazzySep 28.2006 — have you tried it w/ ucfirst?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorSep 29.2006 — That only upper cases the first letter of the string, not every letter of each word.
Copy linkTweet thisAlerts:
@chazzySep 29.2006 — [code=php]
function ucFirstWithParenthesis($string){
$temp = str_replace("(","( ",$string);
$temp = ucwords($temp);
return $temp;
}
[/code]


Not tested.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorSep 29.2006 — It half way worked, so I modifyed it to look like this:


[code=php]function edit_name($edit){
$nedit = str_replace("(","( ",$edit);
$temp = ucwords($nedit);
$nedit = str_replace("( ","(",$temp);
return $nedit;
}[/code]

That works.^
Copy linkTweet thisAlerts:
@chazzySep 29.2006 — knew i forgot something. glad i mentioned not tested and you knew what to do ? cheers!
Copy linkTweet thisAlerts:
@bokehSep 29.2006 — [code=php]<?php


$target = 'a (string with brackets).';
echo preg_replace('/b[a-z]/e', "strtoupper('$0')", $target);

?>[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorSep 29.2006 — Can someone explaine to me what this means: [B]/b[a-z]/e[/B]

and what each of the symbols are for.
Copy linkTweet thisAlerts:
@bokehSep 29.2006 — [B]/[/B]: delimiter

[B]b[/B]: word boundary

[B][a-z][/B]: one character, lower case, a thru z

[B]/[/B]: delimiter

[B]e[/B]: "e" modifier, used with preg_replace, orders the second argument to be evaluated.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorSep 29.2006 — So... If I would like to uppercase the letter after a period would I use this:

preg_replace('/b./e', "strtoupper('$0')", $target);

And if I would like to uppercase the letter after a hyphen would I use this:

preg_replace('/b-/e', "strtoupper('$0')", $target);
Copy linkTweet thisAlerts:
@bokehSep 29.2006 — So... If I would like to uppercase the letter after a period would I use this:

preg_replace('/b./e', "strtoupper('$0')", $target);[/QUOTE]
No! For a start an unescaped dot in PCRE is a wildcard. Also even if it were an escaped dot all you would be capturing would be a dot.

And if I would like to uppercase the letter after a hyphen would I use this:

preg_replace('/b-/e', "strtoupper('$0')", $target);[/QUOTE]
All you are capturing here is a hyphen.

For me to give useful help about this you will need to post a before and after example!
Copy linkTweet thisAlerts:
@The_Little_GuyauthorSep 30.2006 — [B]Before:[/B]

one-x

a.s.a.p.

[B]After:[/B]

One-X

A.S.A.P.
Copy linkTweet thisAlerts:
@bokehSep 30.2006 — That is because every character in that string is at a word boundry and is matched by a wildcard. Doesn't make it right though.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 03.2006 — [code=php]$nedit = preg_replace('/b[a-z]/e', "strtoupper('$0')", $nedit);[/code]

I believe that this is making anything after an [B]'[/B] become uppercase.

[U]Example:[/U]

So the word don't becomes Don'T and I want it to become Don't.

Is there a way to remove that from happening with this line? Otherwise I will add a str_replace.
Copy linkTweet thisAlerts:
@bokehOct 03.2006 — [code=php]$nedit = preg_replace('/(?<!')b[a-z]/e', "strtoupper('$0')", $nedit);[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 03.2006 — Thanks it works, but I did find a little error in it.

I found a song that looked like this:

Kill Rock 'N Roll

the N becomes lowercase in that.

So...

- if the ' comes before the word keep the first letter after it uppercase

- if the ' comes inside the word, keep the letter after it lowercase
Copy linkTweet thisAlerts:
@bokehOct 03.2006 — [code=php]$nedit = preg_replace('/(?<![a-z]')b[a-z]/e', "strtoupper('$0')", $nedit);[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — That doesn't do it, it still makes everything after a [B]'[/B] uppercase
Copy linkTweet thisAlerts:
@bokehOct 04.2006 — That doesn't do it, it still makes everything after a [B]'[/B] uppercase[/QUOTE]You said you wanted

Don't

Rock 'N Roll

to appear like that. That is what that expression does. Maybe you didn't explain something properly.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — Then why doesn't it work?

And yes... I do want:

Don't

Can't

Shouldn't

Wouldn't

That's

Rock 'N Roll

'Bout

'Round

L'esprit D'escalier
Copy linkTweet thisAlerts:
@bokehOct 04.2006 — [code=php]<?php

$nedit = "don't! rock 'n roll!";

echo $nedit = preg_replace('/(?<![a-z]')b[a-z]/e', "strtoupper('$0')", $nedit);

# echos: Don't! Rock 'N Roll!

?>[/code]
What is the problem with that? Isn't that what you asked for?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — Don't

Can't

Shouldn't

Wouldn't

That's

Rock 'N Roll

'Bout

'Round

L'esprit D'escalier

I get and don't want:

Don'T

Can'T

Shouldn'T

Wouldn'T

That'S

Rock 'N Roll

'Bout

'Round

L'Esprit D'Escalier
Copy linkTweet thisAlerts:
@bokehOct 04.2006 — Ok, so where is the conceived problem?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — Maybe its the function:
[code=php]function move_end($text)
{
return(preg_replace('/^(a|an|the)s+(.+)/i', '\2, \1', $text));
}
function edit_name($edit){
$remove = array("/", "&", " ");
$replace = array("", "And", " ");
$nedit = move_end(ucwords(addslashes(strtolower(str_replace($remove,$replace,$edit)))));
$nedit = str_replace(".",". ",$nedit);
$temp = ucwords($nedit);
$nedit = str_replace(". ",".",$temp);
$nedit = str_replace("-","- ",$nedit);
$temp = ucwords($nedit);
$nedit = str_replace("- ","-",$temp);
$nedit = preg_replace('/(?<![a-z]')b[a-z]/e', "strtoupper('$0')", $nedit);
return $nedit;
}[/code]
Copy linkTweet thisAlerts:
@bokehOct 04.2006 — I'm not sure. Too much code for my liking. Most of it redundant. Start by stripping out all the calls to UCWORDS.
Copy linkTweet thisAlerts:
@bokehOct 04.2006 — Ok! Here's my take on it. Assuming the addslashes is to escape the DB (but really you should use the custom escape function for your D?:[code=php]function edit_name($nedit)
{
$nedit = strtolower($nedit);
$nedit = str_replace(array("/", "&", " "), array("", "and", " "), $nedit);
$nedit = preg_replace('/^(a|an|the)s+(.+)/i', '$2, $1', $nedit);
$nedit = preg_replace('/(?<![a-z]')b[a-z]/e', "strtoupper('$0')", $nedit);
return addslashes($nedit);
}[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — Nope, that doesn't work for me.
Copy linkTweet thisAlerts:
@bokehOct 04.2006 — "[I]Nope, that doesn't work for me.[/I]" That type of statement is not very helpful to your cause. Provide the string that is problematic, the expected result and the obtained result.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — Ok... I take that back it does work here: http://d-top.org/la.php

but it doesn't work in the actual application.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — "[I]Nope, that doesn't work for me.[/I]" That type of statement is not very helpful to your cause. Provide the string that is problematic, the expected result and the obtained result.[/QUOTE]

http://d-top.org/la.php
Copy linkTweet thisAlerts:
@bokehOct 04.2006 — I really don't see what you are complaining about. This code mimics your desired result exactly.[code=php]<?php

function edit_name($nedit)
{
$nedit = strtolower($nedit);
$nedit = str_replace(array("/", "&", " "), array("", "and", " "), $nedit);
$nedit = preg_replace('/^(a|an|the)s+(.+)/i', '$2, $1', $nedit);
$nedit = preg_replace('/(?<![a-z]')b[a-z]/e', "strtoupper('$0')", $nedit);
return addslashes($nedit);
}

$tests = array("don't! rock 'n roll",
"l'esprit d'escalier",
"'bout",
"that's",
"one-x",
"l.d.n."
);

foreach($tests as $test)
{
echo edit_name($test)."<br>n";
}

?>[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 04.2006 — Im saying that it isn't working on my actual site.
Copy linkTweet thisAlerts:
@bokehOct 05.2006 — Well it's not this code at fault, so you will have start looking elsewhere for the error.
Copy linkTweet thisAlerts:
@mickeycAug 06.2010 — That code is okay when echoing something... like:

echo edit_name('christian's inferno');

the result is "Christian's Inferno".

but, when inserting or updating on a sql the result is like this:

$name = 'christian's inferno';

$name = edit_name($name);

mysql_query('UPDATE some_table SET name='".$name."' ');

the result in the db is "Christian'S Inferno".

Why do this happens???
Copy linkTweet thisAlerts:
@Declan1991Aug 06.2010 — That code you've posted works? Because you seem to have an awful amount of badly nested quotes.
Copy linkTweet thisAlerts:
@mickeycAug 06.2010 — yeah it works, i like working in php with ' instead " ....for reasons like <a href""> i like the "" quotes in that way... but yes it works
Copy linkTweet thisAlerts:
@mickeycAug 06.2010 — Correcting...

$name = "christian's inferno";

echo edit_name($name);

the result is "Christian's Inferno".

but, when inserting or updating on a sql the result is like this:

mysql_query('UPDATE some_table SET name='".$name."' ');

the result in the db is "Christian'S Inferno".

Why do this happens???
Copy linkTweet thisAlerts:
@mickeycAug 06.2010 — Correcting AGAIN...

$name = "christian's inferno";

echo edit_name($name);

the result is "Christian's Inferno".

but, when inserting or updating on a sql the result is like this:

$name = edit_name($name);

mysql_query('UPDATE some_table SET name='".$name."' ');

the result in the db is "Christian'S Inferno".

Why do this happens???
Copy linkTweet thisAlerts:
@NogDogAug 06.2010 — No idea why it would be different. I would suggest starting by creating the query string, then echoing it out separately to confirm that it looks how you expect. Also make sure you check that the query succeeded and mysql_affected_rows() is not zero, so that you are not just seeing an unchanged DB value.
×

Success!

Help @The_Little_Guy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...