duangangpin078794 2009-09-13 11:50
浏览 29
已采纳

获取MySql行的一部分?

Is there any way to get just a part from MySql cell and ignore everything after predefined character? For example, I have row with cell's "ID, LINK, PHONE". Inside LINK cell DB wrights links like mysite.com/mypicture.jpeg. Is there any way, to get just "mysite.com" from this cell and ignore everything after slash, so I can echo it inside anchor tag?

Edit: To be absolutely clearly I'm posting the existing code:

<?PHP
if(mysql_connect($db_host,$db_user,$db_pass)) {
    mysql_select_db($db_name);
    mysql_query("CREATE TABLE IF NOT EXISTS smsads(id bigint unsigned primary key auto_increment, link varchar(255), fromnum varchar(60))");
    $res = mysql_query("SELECT * FROM smsads ORDER BY id DESC LIMIT 3");
    while($row = mysql_fetch_object($res)) {
        $http_link = $row->link;
        if(strstr($http_link, 'http') === FALSE) $http_link = 'http://'.$http_link;
        echo "<div id=\"banner\"><a href=\"{$http_link}\" target=\"_blank\"><img src=\"{$http_link}\" /></a></div>";
    }
}
?>
  • 写回答

4条回答 默认 最新

  • dousi6701 2009-09-13 11:54
    关注

    A solution, on the PHP side, might be to use strpos and substr, to :

    • find the position of the character
    • and extract what comes before/after.

    A bit like this, for instance :

    $str = 'mysite.com/mypicture.jpeg';
    
    $position = strpos($str, '/');
    if ($position !== false) {
        $before = substr($str, 0, $position);
        $after = substr($str, $position+1);
        var_dump($before, $after);
    }
    

    Which will get you :

    string 'mysite.com' (length=10)
    string 'mypicture.jpeg' (length=14)
    


    If you know there will always be one (and only one) slash in your data, you can also use explode and list :

    list($before, $after) = explode('/', $str);
    var_dump($before, $after);
    

    Which will give you the same output :

    string 'mysite.com' (length=10)
    string 'mypicture.jpeg' (length=14)
    


    Another idea would be to do that on the SQL side -- if you need both fields, though, doing it on the PHP side is not a bad idea (both sides, PHP and SQL, are valid, actually).


    EDIT after the comments.

    What about something like this, for your loop :

    while($row = mysql_fetch_object($res)) {
        $http_link = $row->link;
        $position = strpos($http_link, '/');
        if ($position !== false) {
            $before = substr($http_link, 0, $position);
            if(strstr($http_link, 'http') === FALSE) {
                $http_link = 'http://'.$http_link;
                $before = 'http://' . $before;
            }
            echo "<div id=\"banner\"><a href=\"{$before}\" target=\"_blank\"><img src=\"{$http_link}\" /></a></div>";
        }
    }
    

    For each line, you are getting the $row->link to the $http_link variable, like you did before.

    Then :

    • you extract the part that comes before the '/' ; and use it for the a href tag, to link to the root of the website
    • you use the full URL from the DB, for the img src tag, to display the image

    And you don't forget to add 'http://' if necessary to both URLs, like you did at the first place when you only had one.

    Note : that code is not tested, but should give you a hint of a possible solution.

    Hope this helps :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序