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 ?
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:
Alternatively you can just use the ltrim function: <?php rtrim("vasil76|12352 petr0|54323 sidor80|22222 egorf|55555 andrey|78787878 max123|909091 |", " |"); PHP:
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.
Exactly - fixing something that is inherently broken just adds code - usually fixing the underlying issue is the better bet.
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 ?
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: