joshgarrod Posted December 18, 2010 Share Posted December 18, 2010 Hi, I am trying to search a string for a string of characters in this order ../ and carry out relevant if statements but it returns the same result every time. On echoing my $pos, $pos2, $pos3 and $pos4 variables the first two come back 0 and they are the strings that do contain ../ but the if statements perform the same? I don't understand, any ideas? Thanks... <?php $logo = $info ['logo']; $pos = strpos($logo, "../");//check if image contains ../ if ($pos == 0) {} else { $logo = substr($logo, 3);} $image1 = $info ['image1']; $pos1 = strpos($image1, "../");//check if image contains ../ if ($pos1 == 0) {} else { $image1 = substr($image1, 3);} $image2 = $info ['image2']; $pos2 = strpos($image2, "../");//check if image contains ../ if ($pos2 == 0) {} else { $image2 = substr($image2, 3);} $image3 = $info ['image3']; $pos3 = strpos($image3, "../"); //check if image contains ../ if ($pos3 == 0) {} else { $image3 = substr($image3, 3);} ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 18, 2010 Share Posted December 18, 2010 you might consider leaving out the empty if parts //if ($pos == 0) {} //else { // Replace with if ($pos != 0) { Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted December 18, 2010 Author Share Posted December 18, 2010 i still does the same, is there any other way of checking if the string contains ../ and if it does strip it off? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 18, 2010 Share Posted December 18, 2010 yes, but it hurt my eyes when i tried to read your code so i had to stop. can we see the updated code? Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted December 18, 2010 Author Share Posted December 18, 2010 Sure: <?php $logo = $info ['logo']; $pos = strpos($logo, "../");//check if image contains ../ if ($pos != 0) {} else { $logo = substr($logo, 3);} $image1 = $info ['image1']; $pos1 = strpos($image1, "../");//check if image contains ../ if ($pos1 != 0) {} else { $image1 = substr($image1, 3);} $image2 = $info ['image2']; $pos2 = strpos($image2, "../");//check if image contains ../ if ($pos2 != 0) {} else { $image2 = substr($image2, 3);} $image3 = $info ['image3']; $pos3 = strpos($image3, "../"); //check if image contains ../ if ($pos3 != 0) {} else { $image3 = substr($image3, 3);} ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 18, 2010 Share Posted December 18, 2010 hm, now the logic is different. it looks like you just changed == to !=, but still left the empty brackets, {} and the else. it really helps readability if you remove the unnecessary empty brackets. easier to read == easier to fix, and can we see what's in $logo? <?php $logo = $info['logo']; echo "logo: $logo <br />"; $pos = strpos($logo, "../");//check if image contains ../ if ($pos != 0) { $logo = substr($logo, 3); } $image1 = $info ['image1']; $pos1 = strpos($image1, "../");//check if image contains ../ if ($pos1 != 0) { $image1 = substr($image1, 3); } $image2 = $info ['image2']; $pos2 = strpos($image2, "../");//check if image contains ../ if ($pos2 != 0) { $image2 = substr($image2, 3); } $image3 = $info ['image3']; $pos3 = strpos($image3, "../"); //check if image contains ../ if ($pos3 != 0) { $image3 = substr($image3, 3); } ?> Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted December 18, 2010 Share Posted December 18, 2010 Alteratively use could always use Ternary Operators Link: http://www.totallyphp.co.uk/tutorials/using_if_else_ternary_operators.htm Link: http://www.php.net/manual/en/language.operators.comparison.php <?PHP $logo = strstr($info['logo'],'../') ? substr($info['logo'], 3) : 'NULL'; $image1 = strstr($info['image1'],'../') ? substr($info['image1'], 3) : 'NULL'; $image2 = strstr($info['image2'],'../') ? substr($info['image2'], 3) : 'NULL'; $image3 = strstr($info['image3'],'../') ? substr($info['image3'], 3) : 'NULL'; echo '<pre>'; echo 'Logo: '.$logo.'<br>'; echo 'Image 1: '.$image1.'<br>'; echo 'Image 2: '.$image2.'<br>'; echo 'Image 3: '.$image3.'<br>'; ?> Regards, Paul. Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted December 18, 2010 Author Share Posted December 18, 2010 Hi BlueSkyIS, $logo contains: logo: ../uploads/pretendloog.jpg - with those alterations it performs as before. Hi Paul Ryan, that almost works but how do I stop NULL from replacing the contents of the variable if ../ is not present? Thanks for your time all Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted December 18, 2010 Share Posted December 18, 2010 Just replace 'Null' with '' that should leave the variable blank Regards, Paul. Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted December 19, 2010 Author Share Posted December 19, 2010 Yes but I dont want to change the contents to anything if it doesn't contain ../ - can I just tell it do nothing? Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted December 19, 2010 Share Posted December 19, 2010 This is what you want: - If the image contains ../ you can to remove it and set the variable with the remainder - If the image does not contain ../ you can to keep the orginal value? Regards, Paul. Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted December 19, 2010 Author Share Posted December 19, 2010 exactly, basically stripping off the ../ from the image path if it has one, if not then leave the image path Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted December 19, 2010 Share Posted December 19, 2010 Simple just put the original array element as the result if it doesn't contain the string you're looking for. <?PHP $logo = strstr($info['logo'],'../') ? substr($info['logo'], 3) : $info['logo']; $image1 = strstr($info['image1'],'../') ? substr($info['image1'], 3) : $info['image1']; $image2 = strstr($info['image2'],'../') ? substr($info['image2'], 3) : $info['image2']; $image3 = strstr($info['image3'],'../') ? substr($info['image3'], 3) : $info['image3']; echo '<pre>'; echo 'Logo: '.$logo.'<br>'; echo 'Image 1: '.$image1.'<br>'; echo 'Image 2: '.$image2.'<br>'; echo 'Image 3: '.$image3.'<br>'; ?> Regards, Paul. Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted December 19, 2010 Author Share Posted December 19, 2010 Awesome thanks, problem solved! Quote Link to comment 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.