Jump to content

Simple Solution I think...


Praetorian46

Recommended Posts

I need help stripping a few characters "0," out of a string but i don't want to it to strip it out if it is "10,"  or "20," or something like that.. here is the code that I am working with now...

I'm kinda new to PHP so it may be a bit messy. ???

 

$Cust_ID = implode(",", $Cust_ID);
$Job_ID = implode(",", $Job_ID);
$Empl_ID = implode(",", $Empl_ID);
$Class_ID = implode(",", $Class_ID);

if($Cust_ID!="" AND $Cust_ID!="0") $Message = 'Cust_ID:  ' . $Cust_ID;
if($Job_ID!="" AND $Job_ID!="0") $Message .= '<br> Job_ID:  ' . $Job_ID;
if($Empl_ID!="" AND $Empl_ID!="0") $Message .= '<br> Empl_ID:  ' . $Empl_ID;
if($Class_ID!="" AND $Class_ID!="0") $Message .= '<br> Class_ID:  ' . $Class_ID;

Die($Message);

 

That outputs this...

 

Cust_ID: 0,3,4,5,1,6,7,8,9,10,11,16,15,14,17,12,13,2
Job_ID: 0,3,1,2
Empl_ID: 0,2,1
Class_ID: 0,1,2

 

I'm getting the arrays by having a bunch of values selected in a MULTIPLE SELECT box.  If the user selects the ID "0" I want that to be ignored since ID "0" is the word "ANY" and will be left out of the query I'm going to run with this data...  I hope I provided enough info for you to help me out.

Thank you,

-Cody

Link to comment
https://forums.phpfreaks.com/topic/82532-simple-solution-i-think/
Share on other sites

use str_replace to replace any ,0 with an empty string ''

Then check if the very first character of the string is 0 and if so remove it and the subsequent comma.

 

Or you could loop through the array before you implode it and unset any values == 0

Well because if they click anywhere in the box  it selects something...  So in order for them to unselect it they would have to {Crtl} Click on what ever they clicked on.  These guys are idiots that are going to be using this script.  It's easier to just exclude the 0 that is selected.. At least I think it is.  Maybe the issue I am having isn't as easy as I thought.

use str_replace to replace any ,0 with an empty string ''

Then check if the very first character of the string is 0 and if so remove it and the subsequent comma.

 

Or you could loop through the array before you implode it and unset any values == 0

 

The looping through the array seems to be the easiest way to go about this... I don't know why I didn't think of that  :-\  I knew it was something simple I was just over looking.  Thanks a bunch Jesirose! :)

Archived

This topic is now archived and is closed to further replies.

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