Jump to content

[SOLVED] won't get part of string


Sesquipedalian

Recommended Posts

It seemed to work in other cases, but for some reason when I changed it to what im going to use it for, it won't work... so what am i doing wrong?

 

<?
ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);
$user = 'adam';
$a = '|'.'adam'.'sesq'.'|';
if ($start=strpos($a, '|'.$user)) {
	if ($end=strpos(substr($a,$start+1), '|')) {
		$b = substr($a, $start+1, $end-1);
		echo $b;
	}
} else {
	echo 'error';
}	
?>

 

technically it should display 'adamsesq', because i've done it before, but for some reason it won't?

 

oh, and on a further note, this is part of a much bigger thing, but this is the main part that doesn't seem to work. i just dont get why.

Link to comment
https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/
Share on other sites

it's displaying 'error', which is what happens if it cannot find either the first marker, or the second (the first is '|adam' and the second is '|').

 

Yes, because |adam is located at position 0 in the string. This....

 

if ($start=strpos($a, '|'.$user)) {

 

will return false.

 

What exactly are you wanting to achieve? I just don't see alot of point to your logic.

Uhm. well actually im making a login solely based on textfiles, and this is where it deletes a specific username and password from a textfile.

 

I only know what the username is, and its all encoded in md5, so its something like md5('|').md5($user).md5($pass).md5('|'), then i'm trying to find out what md5($user).md5($pass) is, so I can use str_replace to replace md5($user).md5($pass) with nothing so that the user is deleted...

 

i can post all of the code, but its a lot more, and a lot of other unrelated things.

Is an md5'd version of the username/password combo seperated by | all that is represented by each line in this file?

 

If so it would be as simple as....

 

<?php

  $tmp = array();

  $user = md5('|foo|bar|');

  $lines = file('passwords.txt');
  foreach ($lines as $line) {
    if ($line != $user) {
      $tmp[] = $line;
    }
  }

  file_put_contents('passwords.txt', implode("\n",$tmp));

?>

<?php

  // create an empty temp array to store the lines to keep in.
  $tmp = array();

  // create the md5'd user/password combo to use in our comparison.
  $user = md5('|foo|bar|');

  // save each line of passwords.txt file into an element of an array.
  $lines = file('passwords.txt');
  
  // loop through each line in the array.
  foreach ($lines as $line) {
    // see if the line we are on matches our user/password combo created earlier.
    if ($line != $user) {
      // if it does'nt match, save the line in our temp array of lines to keep.
      $tmp[] = $line;
    }
  }

  // write the lines to keep back over the passords.txt file.
  file_put_contents('passwords.txt', implode("\n",$tmp));

?>

 

Does that help?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.