Jump to content

Keep only numbers at end


siwelis

Recommended Posts

I ended up using a bit of both codes:

 

if(str_replace('img_', '', $key)){
//to only change the $key when img_ is present
$key = substr($key, 4);
//remove img_ from each instance
echo $key."<br />";
echo "<br />Image ".$key." is ON!<br />";
}

 

Thank you!

Ken2k7 has a point, but why do you care if it's there, by simply replacing all instances of 'img_' with '', if there are non in the string it won't replace any, if it is in the string it will be replaced, I'm not sure I see the point of checking first.

Ken2k7 has a point, but why do you care if it's there, by simply replacing all instances of 'img_' with '', if there are non in the string it won't replace any, if it is in the string it will be replaced, I'm not sure I see the point of checking first.

 

I always need "img_" replaced, but there are other things in the array that do not have img_ and I need them left alone...

 

if(str_replace('img_', '', $key)){

The above piece of code didn't do as I wanted... whether it actually replaces anything or not, it carries out the following code.

I tried it with strpos and it executed everything in between the brackets whether it actually did anything or not...

 

This worked though:

 

if(substr_count($key,'img_') == 1){
//if occurences of "img_" = 1
$key = substr($key, 4);
//remove img_
echo $key."<br />";
//verify removal
echo "Image ".$key." is ON!<br /><br />";
//party time
}

 

Thank you again!

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.