In php many function available to return string in rever order but many times in interview as some question write down simple string rever function wihtout str_reverse() function
PHP string reverse function
Demo link :
Code link :
Syntax :
strrev(element);
Eg :
<?php echo strrev("Hello"); ?>
Output :
olleH
strrev() is return string rever order but without use function following program is return string is reverse manner :
<?php
$original_str=”disuzajen”]; $reserve_name=str_split($original_str);
$strlength=strlen($original_str);
//
for($i=$strlength-1;$i>=0;$i--)
{
//show string one by one::-
echo $reserve_name[$i];
}
?>
This function above php4 version.
Following file structure create :
1-header.php
2-footer.php
3-index.php
1-header.php :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>String reverse in php</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<!-- start:header -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href="#">Free Teachnology</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home <span class="sr-only"></span></a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Contact us</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</br></br></br>
<!-- end:header -->
2-footer.php
Code :
<script>
// $(".errorMsg").alert('close');
$(".errorMsg").slideUp(2000);
</script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<!-- Start::footer -->
<footer>
<div class="footer footer-bottom">
<div class="container">
<p class="pull-left"> Copyright © <?php echo date('Y');?>. Design and Develop by - Disuza Jen. </p>
<div class="pull-right">
<ul class="nav nav-pills payments">
<li><i class="fa fa-facebook"></i></li>
<li><i class="fa fa-google"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-link"></i></li>
</ul>
</div>
</div>
</div>
<!--/.footer-bottom-->
</footer>
<!-- End::footer -->
<style type="text/css">
.footer-bottom {
margin-top:220px;
background: #E3E3E3;
border-top: 1px solid #DDDDDD;
padding-top: 10px;
padding-bottom: 10px;
}
.footer-bottom p.pull-left {
padding-top: 6px;
}
</style>
</body>
</html>
3-index.php :
<?php include('header.php');?>
<body>
<div class="container">
<div class="alert alert-success">
<a href="#">String Reverse</a>!
</div>
</div>
<div class="clearfix"></div>
<div class="container">
<div class="alert alert-warning">
<a href="https://drive.google.com/open?id=0BxmTZPVcu72fM2RndlU4ejV6LWc" class="btn btn-xs btn-warning pull-right" target="blank();">Click</a>
<strong> Code download link-</strong>
</div>
<div class="row col-lg-10">
<div class="panel panel-default user-add-edit">
<div class="panel-heading">Add Name </div>
<div class="panel-body">
<form class="form-horizontal" action="" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="First name" required="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" name="submit" value="Submit"/>
<input type="reset" class="btn btn-default" value="Cancel" />
</div>
</div>
</form>
<?php if(isset($_POST['firstname'])) { ?>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<label for="inputEmail3" class=" control-label"> New reverse string is :
<?php
$original_str=$_POST['firstname']; $reserve_name=str_split($original_str);
$strlength=strlen($original_str);
//
for($i=$strlength-1;$i>=0;$i--)
{
//show string one by one::-
echo $reserve_name[$i];
}
?>
</label>
</div>
</div>
<?php }?>
</div>
</div>
</div>
</div>
<?php include('footer.php');?>
Thanks for supporting me and please comment and share to other people to help us.
Comments