Jump to content

Search string for characters


joshgarrod

Recommended Posts

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.