Jump to content

Recommended Posts

Hi guys,

First post here. Straight into it.

 

I am wondering if i can get some help. I know how to use the IF statement for a full string, however i am looking for it to check the start of a string. It is a project for work, where there are files that come in, these files start with 3 letters and a number followed by a : sign. each department has a different combination and i would it to be able to read that first part and know what department it is. Is this possible?

 

Link to comment
https://forums.phpfreaks.com/topic/77285-use-of-if-statement/
Share on other sites

hey,

you could use the strpof function to locate characters in a sting for instance

 

$find  = 'whatyouarelookingfor;
if (strpos($whatyourlookingin, $find) == true){ //do stuff in here }
else{//do stuff in here}

 

I think that should work. I use the same method in one of my scripts.

If it doesnt work post your code and the output for us to see!

 

eon201

Link to comment
https://forums.phpfreaks.com/topic/77285-use-of-if-statement/#findComment-391286
Share on other sites

Your probably going to need to be a little more specific in the description of what exactly these strings look like, but maybe....

 

<?php

  $s = 'dep2345:';
  preg_match('/^(.*):/',$s,$matches);
  if ($matches[0] == 'dep2345') {
    echo "belongs to dep";
  }

?>

 

helps.

Link to comment
https://forums.phpfreaks.com/topic/77285-use-of-if-statement/#findComment-391298
Share on other sites

This is what i have so far:

 

<?php

 

  $s = $_REQUEST['file];

 

  if (preg_match('/^ITS0001/i',$s))

    echo "<meta http-equiv=refresh content='1;url=Department/ITSales.php?file=";

    echo $_REQUEST['file'];

    echo "' />";

   

  elseif (preg_match('/^PERS0001/i',$s))

    echo "<meta http-equiv=refresh content='1;url=Department/Personnel.php?file=";

    echo $_REQUEST['file'];

    echo "' />";

 

 

  else

    echo "<h1>Not Recognised Format</h1>";

 

?>

 

This works for ITS0001 file, but not for PERS0001. Instead i get an error

 

'Parse error: syntax error, unexpected T_ELSEIF in /home/www/XXXXXXXXXX/target.php on line 16'

Link to comment
https://forums.phpfreaks.com/topic/77285-use-of-if-statement/#findComment-391308
Share on other sites

please put your could in blocks like the following

 

 

<?php

  $s = $_REQUEST['file];
  
  if (preg_match('/^ITS0001/i',$s)) 
  {
    echo "<meta http-equiv=refresh content='1;url=Department/ITSales.php?file=";
    echo $_REQUEST['file'];
    echo "' />";
  } 
  elseif (preg_match('/^PERS0001/i',$s))
  {
    echo "<meta http-equiv=refresh content='1;url=Department/Personnel.php?file=";
    echo $_REQUEST['file'];
    echo "' />";
  }
  else
  {
    echo "<h1>Not Recognised Format</h1>"; 
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/77285-use-of-if-statement/#findComment-391311
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.