如何从javascript字符串的末尾删除可变数量的字符

时间:2019-03-13 00:43:01

标签: javascript string variables slice

我一直在尝试使类似的东西起作用,但它不会:

<?php include('header.php');
    $qry2=mysqli_query($con,"select * from tbl_movie where movie_id='".$_GET['id']."'");
    $movie=mysqli_fetch_array($qry2);
    ?>
<div class="content">
    <div class="wrap">
        <div class="content-top">
                <div class="section group">
                    <div class="about span_1_of_2">
                        <h3><?php echo $movie['movie_name']; ?></h3>
                            <div class="about-top">
                                <div class="grid images_3_of_2">

                                    <img src="<?php echo $movie['image']; ?>" width="180px" height="280px" alt=""/>
<?php include('ratte.php'); ?>
                                </div>
                                <div class="desc span_3_of_2">
                                    <p class="p-link" style="font-size:15px">Type: <?php echo $movie['type']; ?></p>
                                    <p class="p-link" style="font-size:15px">Price: £<?php echo date($movie['price']); ?></p>
                                    <p style="font-size:15px"><?php echo $movie['desc']; ?></p>
                                    <a href="<?php echo $movie['video_url']; ?>" target="_blank" class="watch_but">Watch Trailer</a>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <?php $s=mysqli_query($con,"select DISTINCT theatre_id from tbl_shows where movie_id='".$movie['movie_id']."'");
                            if(mysqli_num_rows($s))
                            {?>
                            <table class="table table-hover table-bordered text-center">
                            <?php

                                while($shw=mysqli_fetch_array($s))
                                {
                                    $t=mysqli_query($con,"select * from tbl_theatre where id='".$shw['theatre_id']."'");
                                    $theatre=mysqli_fetch_array($t);
                                    ?>
                                    <tr>
                                        <td>
                                            <?php echo $theatre['name'].", ".$theatre['place'];?>
                                        </td>
                                        <td>
                                            <?php $tr=mysqli_query($con,"select * from tbl_shows where movie_id='".$movie['movie_id']."' and theatre_id='".$shw['theatre_id']."'");
                                            while($shh=mysqli_fetch_array($tr))
                                            {
                                                $ttm=mysqli_query($con,"select  * from tbl_show_time where st_id='".$shh['st_id']."'");
                                                $ttme=mysqli_fetch_array($ttm);

                                                ?>

                                                <a href="check_login.php?show=<?php echo $shh['s_id'];?>&movie=<?php echo $shh['movie_id'];?>&theatre=<?php echo $shw['theatre_id'];?>"><button class="btn btn-default"><?php echo date('h:i A',strtotime($ttme['start_time']));?></button></a>

                                                <?php
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            ?>
                        </table>
                        <div id='display_comment'></div>
                            <?php
                            }

                            else
                            {
                                ?>
                                <h3>No Show Available</h3>
                                <div id='display_comment'></div>
                                <?php
                            }
                            ?>

                    </div>
                <?php include('related-movies.php');
                ?>
            </div>
                <div class="clear"></div>
            </div>
            <?php include('comments.php'); ?>
    </div>
</div>
<?php include('footer.php'); ?>

我需要从末尾删除可变数量的字符。谢谢!

2 个答案:

答案 0 :(得分:2)

使用slice

var str = "Hello, World";
var chars = 7;
var sliced = str.slice(0, -chars);
console.log(sliced);

答案 1 :(得分:1)

-"variable"是强制逻辑,因此基本上您正在执行以下操作:str.slice(0, NaN);

console.log(-"variable"); // js engine is trying to convert to number

只需使用变量,就不需要用引号将其包装起来,例如字符串。

var variable = 5;
var str = "EleFromStack".slice(0, -variable);
console.log(str)