doumindang2416 2018-05-10 05:46
浏览 38

在php中使用相同名称替换错误

<?php
define("TITLE", "Phrase convertor" );
if (isset( $_POST["fix_submit"]))
{
    //take value from textarea
    $Typed = strtolower( $_POST["typed_phrase"] );
    //store array of  phrases
    $a = array(
        "A bird in the hand is worth two in the bush",
        "A bunch of fives",
        "A chain is only as strong as its weakest link",
        "A diamond in the rough",
        "A diamond is forever"
    );
    $b = array(
        "It's better to have a lesser but certain advantage than the possibility of a greater one that may come to nothing.",
        "A fist The fives are the five fingers.",
        "The proverb has a literal meaning, although the 'weakest link' referred to is figurative and usually applies to a person or technical feature rather than the link of an actual chain.",
        "Someone who is basically good-hearted but lacking social graces and respect for the law.",
        "Advertising slogan for De Beers' diamonds."
    );   
    $actualMeaning = str_replace($a,$b,$Typed);
}
?>
<DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo TITLE; ?></title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
   <div class="container">
       <h1><?php echo TITLE; ?></h1>

       <p class="lead">Type your phrase i gave you actual meaning
       </p>

       <div class="row">
            <form class="col-sm-8 col-sm-offset-2" action="" method="post">

               <textarea placeholder="Type your phrase" class="form-control input-lg" name="typed_phrase"></textarea><br>

                <button type="submit" class="btn btn-primary btn-lg 
               float-right " name="fix_submit" >Meaning!</button>

            </form>
       </div>
       <?php 
       if( isset ( $_POST["fix_submit"] ) )
       {
           echo "<strong class='text-danger'>Your phrase</strong>
           <h4>".ucwords($Typed)."</h4><hr>";
       }
       echo "<strong class='text-success'>Meaning of phrase </strong>
       <h4>".ucwords($actualMeaning)."</h4>";
       ?>
   </div>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

I want to exchange the word with array a to b but when I run the program I got the same name or words I didn't find any logical error please help me for exchange the phrase with their actual meaning which is in array b if any logical or syntactical error is in the program please ask me in which part I have been correct

while I am declaring all the thing correctly

  • 写回答

1条回答 默认 最新

  • dongmei2956 2018-05-10 05:57
    关注

    You are using strtolower on the input, which makes it lower case, but both of $a and $b contain upper case letters. to do so, you will have to use str_ireplace() which is a Case-insensitive version of str_replace().

    read more : http://php.net/manual/en/function.str-ireplace.php

    example :

    <?php
        //take value from textarea
        $Typed =  $_POST["typed_phrase"];
        //store array of  phrases
        $a = array(
            "A bird in the hand is worth two in the bush",
            "A bunch of fives",
            "A chain is only as strong as its weakest link",
            "A diamond in the rough",
            "A diamond is forever"
        );
        $b = array(
            "It's better to have a lesser but certain advantage than the possibility of a greater one that may come to nothing.",
            "A fist The fives are the five fingers.",
            "The proverb has a literal meaning, although the 'weakest link' referred to is figurative and usually applies to a person or technical feature rather than the link of an actual chain.",
            "Someone who is basically good hearted but lacking social graces and respect for the law.",
            "Advertising slogan for De Beers' diamonds."
        );   
        $actualMeaning = str_ireplace($a,$b,$Typed);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算