remove last charcter

Discussion in 'PHP' started by ssimon171078, May 16, 2015.

  1. #1
    i have string :
    vasil76|12352 petr0|54323 sidor80|22222 egorf|55555 andrey|78787878 max123|909091 |
    i want to receive string without last character |:
    vasil76|12351 petr0|54323 sidor80|22222 egorf|55555 andrey|78787878 max123|909091
    how can i do this in php ?
     
    ssimon171078, May 16, 2015 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    Simple with substr

    
    substr("vasil76|12352 petr0|54323 sidor80|22222 egorf|55555 andrey|78787878 max123|909091 |", -1);
    //You could trim the white space if needed
    trim(substr("vasil76|12352 petr0|54323 sidor80|22222 egorf|55555 andrey|78787878 max123|909091 |", -1));
    
    PHP:
     
    Anveto, May 16, 2015 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Alternatively you can just use the ltrim function:

    
    <?php
    rtrim("vasil76|12352 petr0|54323 sidor80|22222 egorf|55555 andrey|78787878 max123|909091 |", " |");
    
    PHP:
     
    Last edited: May 16, 2015
    ThePHPMaster, May 16, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Dunno where you get the string from, but wouldn't the best bet be to make sure the string doesn't contain the last | - I'm assuming this is a concoction of something, adding values to a string (or parsing an array, or something like that) - I'm pretty sure it's possible to avoid that extra space and | at the end, if you just tell us how you make that string.
     
    PoPSiCLe, May 16, 2015 IP
  5. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #5
    Ah true, looks like a textfile with usernames and passwords
     
    Anveto, May 16, 2015 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Exactly - fixing something that is inherently broken just adds code - usually fixing the underlying issue is the better bet.
     
    PoPSiCLe, May 16, 2015 IP
  7. ssimon171078

    ssimon171078 Well-Known Member

    Messages:
    277
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    i have data file :data.txt:
    vasil76|12358
    petr0|54323
    sidor80|22222
    egorf|55555
    andrey|78787878
    max123|909091
    and php script:
    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <?php
    $Iam = htmlspecialchars($_SERVER["PHP_SELF"]);
    $i=0;
    $user_password=array();
    $user_username=array();
    $filename=      "data.txt";
    if (file_exists($filename)){
       
    
    $content=file($filename,FILE_IGNORE_NEW_LINES);
    foreach($content as $line){
    //echo $line.  "<br>";
       $pos=strpos($line,"|");
       $username=substr($line,0,$pos);
       $password=substr($line,$pos+1,strlen($line)-$pos);
    //$pos=explode(":",$line);
    //$pos2=explode( "|",$line);
      //echo $username;
      $user_username[]= $username;
      $user_password[]=$password;
      }
    $user_pass=array_combine($user_username,$user_password);}
    else {
        echo "File $filename doesn't exists";
    }
      //print_r($user_pass);
    
    
      ?>
    <div>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
        <p>
            <label for="userSel">Select user:</label>
            <select id="userSel" name="userSelected">
                <option selected value="">Select user</option>
                <?php
                foreach($user_pass as $key=>$key){
                    echo "<option value='".$key."'>".$key."</option>";
                }
                ?>
            </select>
        </p>
        <p>
            <input type="submit" value="Go">
        </p>
    </form>
    </div>
    <div>
        <?php
       
        if(isset($_POST["userSelected"])){
            $nm = trim($_POST["userSelected"]);
            //echo  $nm ;
            if($nm != ""){
                $number = trim($user_pass[$nm]);
                //echo $number;
                $txtErr = "<form action='$Iam' method='post'>";
                $txtErr .= "<p> Name <input name='nm' name='nm' value='" . $nm . "' ></p>";
                $txtErr .= "<p> Number <input type='number' name='number'  step='1' value='" . $number . "'></p>";
               
                $txtErr .= "<p><input type='submit' value='Submit' name='Submit'></p></form>";
            } else {
                $txtErr = "choose name";
            }
        }
       
        ?>
    </div>
    <div>
        <?php
         echo $txtErr;
        ?>
    </div>
    <div>
    <?php
    if (isset($_POST["Submit"])){
            $new_number=trim($_POST["number"]);
    echo $new_number;}
       
      $user_pass[$_POST["nm"]]=$new_number;
      //print_r($user_pass);
      foreach($user_pass as $key=>$val){
            $new_content.=$key."|".$val."\r\n";
        }
        //echo $new_content;
     
       $file = fopen($filename,"r+");
        fwrite($file,$new_content);
        rewind($file);
        fclose($file);
      
    ?>
    </div>
      </body>
    </html>
    PHP:
    i receive data file :
    vasil76|12350
    petr0|54323
    sidor80|22222
    egorf|55555
    andrey|78787878
    max123|909091

    |
    how to remove last "|" from data file when script wrote to data file ?
     
    ssimon171078, May 17, 2015 IP
  8. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #8
    You could make sure there is a username or password.

    
    foreach($user_pass as $key=>$val){
        if (strlen(trim($key)) > 0 || strlen(trim($val)) > 0) {
            $new_content.=$key."|".$val."\r\n";
        }
    }
    
    PHP:
     
    Anveto, May 17, 2015 IP