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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));

?>

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.