How to check if the first letter of URL is less than G?
I am looking to echo a list of links based on the URL of a websit开发者_StackOverflow中文版e. I was wondering if you could create an if/else to echo different lists based on the first letter of the domain of a site. So basically, if the domain starts with any letter before G, it would echo my first list, and if it is any letter after G, it would echo something else.
<?php
$str = "glue";
if ($str < "g"){
//do stuff
echo("yup");
}
$str = "fluor";
if ($str < "g"){
//do stuff
echo("yup2");
}
for your case
<?php
$url = parse_url($_GET['url']);
$str = $url['host'];
echo $str;
if ($str < "g"){
//do stuff
echo(" has first character lower than g");
}
else{
echo(" has not first character lower than g");
}
- http://sandbox.phpcode.eu/g/99426.php?url=http://gamingcreation.org
- http://sandbox.phpcode.eu/g/99426.php?url=http://famingcreation.org
just an idea:
$range_array = range('A','F');
if(in_array($foo,$range_array){
...
}
You can convert a character to its ascii-value using ord()
:
<?php
$ascii_value = ord($link[0]);
if( $ascii_value >= 65 && $ascii_value < 72 ) echo 'Foo';
else echo 'Bar';
?>
My RegEx solution:
/^[a-f].*/gim
Try this:
$url = "http://www.gaa.com";
$parseURL = parse_url($url);
$host = str_replace("www.","", $parseURL['host']);
if($host[0] <= "g") {
//do something..
}else {
//...
}
<?php
$firstLetter = substr(strtoupper($your_url), 0, 1);
if(ord($firstLetter) >= 65 && ord($firstLetter) <= 71) {
// ... first letter is an A, B, C etc. including G
}
else if (ord($firstLetter) >= 72 && ord($firstLetter) <= 90) {
// ... first letter is H or a later character
}
else {
// ... first letter is not a letter of the alphabet
}
?>
精彩评论