Jump to content

Stripping characters from a variable


globex

Recommended Posts

Hello,

 

I have a variable which holds two numbers separated by an 'x'. Like this: 100x100, or 80x34 or 480x12. How do I:

 

a) strip the first number and the x to get just the second number

b) strip the x and the last number to get just the first number

 

I'm very new at PHP so some details and simple explanations would be fantastic. Or maybe just point me in the direction of the proper documentation page.

Thanks.

Link to comment
Share on other sites

does substr() not need a static length? as in that example will it assume its always, for example, 123x456 ie. 3 numbers, then x, then 3 numbers?

 

So it wouldn't work for 4 numbers, or 2 numbers

 

I would also use explode personally.

Link to comment
Share on other sites

Amen to Chris.

 

If you used substr with strpos you could find the position of the X and use that, but you would still have to call it twice one to get the beginning and one for the end whereas explode does it in one line and I believe is a simpler way to do it.

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.