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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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.