如何判断URL首字母是否小于G?

发布于 2024-11-28 06:22:50 字数 129 浏览 1 评论 0原文

我希望根据网站的 URL 来回显链接列表。我想知道您是否可以创建一个 if/else 来根据站点域的第一个字母回显不同的列表。所以基本上,如果域以 G 之前的任何字母开头,它会回显我的第一个列表,如果它是 G 之后的任何字母,它会回显其他内容。

I am looking to echo a list of links based on the URL of a website. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

二货你真萌 2024-12-05 06:22:51

我的正则表达式解决方案:

/^[a-f].*/gim

My RegEx solution:

/^[a-f].*/gim
紫﹏色ふ单纯 2024-12-05 06:22:51

试试这个:

   $url = "http://www.gaa.com";
    $parseURL = parse_url($url);
    $host = str_replace("www.","", $parseURL['host']);
    if($host[0] <= "g") {
        //do something.. 
    }else {
        //...
    }

Try this:

   $url = "http://www.gaa.com";
    $parseURL = parse_url($url);
    $host = str_replace("www.","", $parseURL['host']);
    if($host[0] <= "g") {
        //do something.. 
    }else {
        //...
    }
梅窗月明清似水 2024-12-05 06:22:51
<?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
}
?>
<?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
}
?>
兔姬 2024-12-05 06:22:50
<?php
$str = "glue";
if ($str < "g"){
    //do stuff
    echo("yup");
}

$str = "fluor";
if ($str < "g"){
    //do stuff
    echo("yup2");
}

对于您的情况

<?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"); 
}
<?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"); 
}
甜尕妞 2024-12-05 06:22:50

只是一个想法:

$range_array = range('A','F');

if(in_array($foo,$range_array){
...
}

just an idea:

$range_array = range('A','F');

if(in_array($foo,$range_array){
...
}
花落人断肠 2024-12-05 06:22:50

您可以使用 ord() 将字符转换为其 ascii 值:

<?php 
    $ascii_value = ord($link[0]);
    if( $ascii_value >= 65 && $ascii_value < 72 ) echo 'Foo'; 
    else echo 'Bar'; 
?>

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'; 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文