Sesquipedalian Posted December 26, 2007 Share Posted December 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/ Share on other sites More sharing options...
teng84 Posted December 26, 2007 Share Posted December 26, 2007 i believe it would be alot easier if you will use regex Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423145 Share on other sites More sharing options...
Sesquipedalian Posted December 26, 2007 Author Share Posted December 26, 2007 but what about this is wrong? and how do you use regex..? Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423147 Share on other sites More sharing options...
trq Posted December 26, 2007 Share Posted December 26, 2007 technically it should display 'adamsesq', because i've done it before, but for some reason it won't? So what is it displaying? ps: Regex should only be used to match a pattern, not a hardcoded match. Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423149 Share on other sites More sharing options...
Sesquipedalian Posted December 26, 2007 Author Share Posted December 26, 2007 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 '|'). Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423150 Share on other sites More sharing options...
trq Posted December 26, 2007 Share Posted December 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423153 Share on other sites More sharing options...
Sesquipedalian Posted December 26, 2007 Author Share Posted December 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423156 Share on other sites More sharing options...
trq Posted December 26, 2007 Share Posted December 26, 2007 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423165 Share on other sites More sharing options...
Sesquipedalian Posted December 26, 2007 Author Share Posted December 26, 2007 only problem is I don't know how that works... Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423166 Share on other sites More sharing options...
trq Posted December 26, 2007 Share Posted December 26, 2007 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423169 Share on other sites More sharing options...
Sesquipedalian Posted December 26, 2007 Author Share Posted December 26, 2007 Yeah, that helped. Thanks.. I actually thought of another way to do it though, and I think is better for me to use because I fully understand it all. Quote Link to comment https://forums.phpfreaks.com/topic/83188-solved-wont-get-part-of-string/#findComment-423207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.