创建一个find函数来获取字符之间的字符串

时间:2014-08-18 09:19:02

标签: php mysql function return substr

(第一个问题发布到Stack)

我试图创建一个以下功能:

  1. 搜索具有变量值的字符串
  2. 找到该变量值时,它将删除变量值
  3. 之前的任何内容
  4. 并且会删除以下预定字符串后的所有内容
  5. 以下是代码:

    <?php 
    
        $user_id = '5';
        $week_start = "2014-8-01 00:00:00";                             
        $week_end = "2014-09-01 23:59:59";
    
    
        //get the row information
        $result_show_picks = mysql_query("
            SELECT 
            id, uid, pick, date_submitted
            FROM picks_recorded
            WHERE uid = '" . $user_id . "'
            AND date_submitted > '" . $week_start . "'
            AND date_submitted < '" . $week_end . "'
            LIMIT 1
            ", $connection);
    
            if (!$result_show_picks) {
                die("Database query failed: " . mysql_error());
        }
    
    
            while ($row_picks = mysql_fetch_array($result_show_picks)) {
                echo "id = " . $row_picks[id] . "<br />";
                echo "uid = " . $row_picks[uid] . "<br />";
                echo "pick = " . $row_picks[pick] . "<br />";
                echo "date_submitted = " . $row_picks[date_submitted] . "<br />";
    
                /* this is the output 
                    id = 233
                    uid = 5
                    pick = |1046:648|145:66|1348:736|506:334|97:37|710:434|1421:768|361:257|325:235|698:430|1457:796|1142:694
                    date_submitted = 2014-08-18 02:50:00
                */
    
                $pick_input = $row_picks[pick];
            };
    
            function assign_values($pick_input) {
                $str1 = ":";
                $str2 = "|";
                $team_picked = substr($pick_input,$str1,$str2);
                return 
                    $var1 = value1,
                    $var2 = value2,
                    $var3 = value3,
                    etc, etc;
    
                    /* hoped for return will be (but I have no idea if this is even feasible):
                        $pick_1046 = "648",
                        $pick_145 = "66",
                        $pick_1348 = "736",
                        etc, etc;
                    */
            };
        ?>
    

    这就是我想要的方式:

        <?php 
            if($row['this_games_id'] == $pick_1046){
                echo "yes";
            };
        ?>
    

    我希望这是有道理的。如果它很重要,这是一个足球选择网站,只适合朋友,并没有涉及任何类型的赌博。

    感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在您想要获取信息的每个字符串上执行以下操作,您可以获得%start%%end%之间的任何内容。

$start = '%start%';
$end   = '%end%';

$long_string = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.%start%I want this text%end% Tempore quae explicabo tempora eum aspernatur harum, rem itaque cumque ipsum neque!';

$pos1 = (stripos($long_string, $start) + strlen($start));
$pos2 = (stripos($long_string, $end));

$result = substr($long_string, $pos1, ($pos2 - strlen($long_string)));

echo $result;