Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if string contains 1 uppercase letter in PHP

Tags:

string

regex

php

Couldn't find a function for this. I'm assuming I need to use regex?

I'm trying to do html redirects in php in cases where the url contains at least 1 upper case letter.

example: http://www.domain.com/Michael_Jordan needs to be redirected to http://www.domain.com/michael_jordan - only problem is I can't seem to find a script to detect if at least 1 capital letter exists.

like image 449
Bob Cavezza Avatar asked Dec 13 '10 06:12

Bob Cavezza


People also ask

How do you check if a string has a capital letter PHP?

PHP | ctype_upper() Function The ctype_upper() function in PHP used to check each and every character of a given string is in uppercase or not. If the string in upper case then it returns TRUE otherwise returns False.

How do you check if a string contains a number in PHP?

The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.

How do you check if a string is all lowercase?

The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.


2 Answers

Some regular expression should be able to the work, you can use preg_match and [A-Z]

if(preg_match('/[A-Z]/', $domain)){  // There is at least one upper } 
like image 187
RageZ Avatar answered Sep 21 '22 15:09

RageZ


if (strtolower($url) != $url){   //etc... 
like image 39
Tyler Eaves Avatar answered Sep 22 '22 15:09

Tyler Eaves