Advertisement
Guest User

meh..

a guest
May 5th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.03 KB | None | 0 0
  1. <!doctype html>
  2. <html class="no-js" lang="">
  3.     <head>
  4.         <meta charset="utf-8">
  5.         <meta http-equiv="x-ua-compatible" content="ie=edge">
  6.         <title></title>
  7.         <meta name="description" content="">
  8.         <meta name="viewport" content="width=device-width, initial-scale=1">
  9.        
  10.         <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
  11.         <link rel="apple-touch-icon" href="apple-touch-icon.png">
  12.         <!-- Modernizr -->
  13.         <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
  14.         <!-- BootStrap 4 -->
  15.         <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
  16.  
  17.         <style>
  18.             code {
  19.                 background: #eee;
  20.                 padding: 1em 0.5em;
  21.                 border-radius: 4px;
  22.                 margin-bottom: 2em;
  23.                 display: block;
  24.             }
  25.         </style>
  26.     </head>
  27.     <body>
  28.         <!--[if lt IE 8]>
  29.             <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  30.         <![endif]-->
  31.  
  32.         <!-- Add your site or application content here -->
  33.         <main class="container">
  34.             <section class='test-pen'>
  35.                 <h2 class='test-pen__title'>1.2 Extracting Substrings</h2>
  36.                 <h3 class='test-pen__subtitle'>Problem</h3>
  37.                 <p>You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.</p>
  38.                
  39.                 <form action='<?=$_SERVER['PHP_SELF'];?>' method='POST' class='form'>
  40.                     <div class='form-group'>
  41.                         <label for="username">Username:</label>
  42.                         <input type="text" class="form-control" name="username" required>
  43.                     </div>
  44.  
  45.                     <div class="form-group">
  46.                         <label for="pswd">Password:</label>
  47.                         <input type="password" class="form-control" name="pswd" required>
  48.                     </div>
  49.  
  50.                     <div class="form-group">
  51.                         <input type="submit" class="form-control" value="Submit">
  52.                     </div>
  53.                 </form>
  54.                
  55.                 <?php
  56.                     // get the first 8 character substring from the input username
  57.                     if ( isset( $_POST['username'] ) ) $username = $_POST['username'];
  58.                    
  59.                     if ( isset( $username ) ) $substring = substr( $username, 0, 8 );
  60.                     if ( !empty( $substring ) ):
  61.                 ?>
  62.                     <code class='code'>
  63.                         the first 8 characters of the username passed to the form is: <?php echo $substring; ?>
  64.                     </code>
  65.                 <?php endif; ?>
  66.                
  67.                 <h3>Leaving out $start from substr()</h3>
  68.                 <p>If you leave out $length, substr() returns the string from $start to the end of the original string, as shown:</p>
  69.                 <code>
  70.                 <?php
  71.                     print substr('watch out for that tree', 17);
  72.                 ?>
  73.                 </code>
  74.  
  75.                 <h3>$start is larger than $length</h3>
  76.                 <p>If $start is bigger than the length of the string, substr() returns false.</p>
  77.                 <code>
  78.                     <?php
  79.                         $string = 'Dog';
  80.                         $start = strlen($string) + 2;
  81.                         $length = strlen($string);
  82.                         if ( substr($string, $start, $length) === FALSE ) {
  83.                             echo '$start variable is longer than the $length variable.';
  84.                         }
  85.                     ?>
  86.                 </code>
  87.  
  88.                 <h3>$start is negative</h3>
  89.                 <p>If $start is negative, substr() counts back from the end of the string to determine where your substring starts, as shown:</p>
  90.                 <code>
  91.                     <?php
  92.                         print substr( 'watch out for that tree', -6 ) . PHP_EOL;
  93.                         print substr( 'watch out for that tree', -17, 5 ) . PHP_EOL;
  94.                     ?>
  95.                 </code>
  96.             </section>
  97.             <section>
  98.                 <h2>Replacing substrings</h2>
  99.                 <h3>Problem</h3>
  100.                 <p>You want to replace a substring with a different string. For example, you want to obscure all but the last four digits of a credit card number before printing it.</p>
  101.                 <code>
  102.                     <?php
  103.                         define('BREAK_TAG', "<br>");
  104.                         $cc = '4266 8414 2232 8218';
  105.                         function obscure_cc_last_4( $cc_num ) {
  106.                             /*
  107.                             ** substr_replace( $old_str, $new_substr, $start, $length )
  108.                             */
  109.                             return substr_replace( $cc_num, '**** ', -4 );
  110.                         }
  111.                         echo obscure_cc_last_4($cc) . BREAK_TAG;
  112.  
  113.                         function obscure_cc_all_but_last_4( $cc_num ) {
  114.                             return substr_replace( $cc_num, '**** ', 0, strlen($cc_num) - 4 );
  115.                         }
  116.                         print obscure_cc_all_but_last_4($cc);
  117.                     ?>
  118.                 </code>
  119.                 <h3>Discussion</h3>
  120.                 <p>Without the $length argument, substr_replace() replaces everything from $start to the end of the string. If $length is specified, only that many characters are replaced:</p>
  121.                 <code>
  122.                     <?php
  123.  
  124.                         $my_pet = 'My pet is a blue dog.';
  125.                         print substr_replace( $my_pet, 'fish.', strpos($my_pet, 'd') );
  126.                         echo BREAK_TAG;
  127.                         print substr_replace( $my_pet, 'green', strpos($my_pet, 'b'), 4 ); // only replaces up to 4 chars from $start of $old_str (then continues w rest of $old_str)
  128.                         $credit_card = '4111 1111 1111 1111';
  129.                         echo BREAK_TAG;
  130.                         print substr_replace( $credit_card, 'xxxx ', 0, strlen($credit_card) - 4 ); // replace everything at start with new string, except the last 4
  131.                     ?>
  132.                 </code>
  133.  
  134.                 <p>The function substr_replace() is useful when you’ve got text that’s too big to display all at once, and you want to display some of the text with a link to the rest. Example 1-15 displays the first 25 characters of a message with an ellipsis after it as a link to a page that displays more text.</p>
  135.                 <code>
  136.                     <?php
  137.                         /*
  138.                         ** Objective:   Understand DB operations and apis to begin planning databases and data
  139.                         **              structures within applications using PHP. Think about how to manipulate
  140.                         **              data and why it would be useful to users thjat call the db within said app.
  141.                         */
  142.                         function connect_to_db( $servername, $username, $password, $dbname ) {
  143.                             if (! isset($servername) )  $servername = NULL;
  144.                             if (! isset($username) )    $username = NULL;
  145.                             if (! isset($password) )    $password = NULL;
  146.                             if (! isset($dbname) )      $dbname = NULL;
  147.  
  148.                             // Create connection
  149.                             $conn = new mysqli($servername, $username, $password);
  150.  
  151.                             // Check connection
  152.                             if ($conn->connect_error) {
  153.                                 die("Connection failed: " . $conn->connect_error);
  154.                             }
  155.                             echo "Connected successfully" . BREAK_TAG;
  156.                             return $conn;
  157.                         }
  158.  
  159.                         function disconnect( $conn ) {
  160.                             $conn->close();
  161.                             echo "Disconnected from database" . BREAK_TAG;
  162.                         }
  163.  
  164.                         function select_db($conn, $name) {
  165.                             mysqli_select_db($conn, $name) or die(mysql_error() . 'ln:160');
  166.                         }
  167.  
  168.                         function insert_message( $conn, $id, $message ) {
  169.                             $query = "INSERT INTO messages (`id`, `message`) VALUES ('$id', '$message')";
  170.  
  171.                             if (mysqli_query($conn, $query) == TRUE)
  172.                                 echo "Query successful!" . BREAK_TAG;
  173.                             else
  174.                                 die(mysqli_error($conn));
  175.                         }
  176.  
  177.                         function render_message_for_user($conn, $id) {
  178.                             $result = $conn->query("SELECT id,message FROM messages WHERE id=$id") or die();
  179.                             printf("Select returned %d rows.\n", $result->num_rows);
  180.  
  181.                             // TODO: this shit...
  182.                             //$message = $conn->query("SELECT message FROM messages WHERE id=$id");
  183.                             // $message_result = mysql_fetch_object($message);
  184.                             ?>
  185.                             <code>
  186.                                 <?= $data; ?>
  187.                             </code>
  188.                             <table class="table">
  189.                                 <thead>
  190.                                     <tr>
  191.                                         <th scope="col">ID:</th>
  192.                                         <th scope="col">MESSAGE:</th>
  193.                                     </tr>
  194.                                 </thead>
  195.                                 <tbody>
  196.                                     <tr>
  197.                                         <th scope="row"><?= $id; ?></th>
  198.                                     </tr>
  199.                                     <tr>
  200.                                         <th scope="row"><?= $message; ?></th>
  201.                                     </tr>
  202.                                 </tbody>
  203.                             </table>
  204.                             <?php
  205.                         }
  206.  
  207.  
  208.  
  209.                         $mysqli = connect_to_db( 'localhost', 'root', 'NEYybep7fzu3$', 'test' );
  210.                         select_db($mysqli, 'test');
  211.                         if ( $mysqli->connect_errno )
  212.                             printf( "Connection Failure: %s" . BREAK_TAG, $mysqli->connect_error );
  213.                         else {
  214.                             $msg = "ER AIN\'T NO TIME FOR DISCUSSION! THE ART OF WAR IS MY THEORY I LOVE IT!";
  215.                             insert_message( $mysqli, 1, $msg );
  216.                             render_message_for_user($mysqli, 1);
  217.                         }
  218.                         disconnect($mysqli);
  219.                     ?>
  220.                 </code>
  221.             </section>
  222.         </main>
  223.  
  224.         <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
  225.         <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
  226.         <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.12.4.min.js"><\/script>')</script>
  227.  
  228.         <!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
  229.         <script>
  230.             window.ga=function(){ga.q.push(arguments)};ga.q=[];ga.l=+new Date;
  231.             ga('create','UA-XXXXX-Y','auto');ga('send','pageview')
  232.         </script>
  233.         <script src="https://www.google-analytics.com/analytics.js" async defer></script>
  234.     </body>
  235. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement