Unstoppable Domains

Php: substr_replace question

Spaceship Spaceship
Watch

TeviH

Established Member
Impact
2
I'm really a noob at php, and I'm puzzled by a small dilemma:

I have a form mail system, and all fields are assigned a variable creatively named $variable. The form is an order submission (no payments or such), so those 23 fields which represent products have a prefix to their value of "val" (another demonstration of my blinding creativity!! :hehe: ). So if I wanted to submit an order for 5 widgets, the value would actually equal "val5".

Now, when the form gets emailed, I don't want it to display "widgets: val5". I want it to only display "widgets: 5".

So, I entered the following code into my php function which compiles all the information:
PHP:
build_message(substr_replace($value, '', 0, 3)

That works like a charm except for one problem - other fields, such as "name" "email" "comments" etc, get the first 3 characters lopped off.

So, how do I only get rid of the substring "var", rather than the first 3 characters of every single field??
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
if (preg_match('#val([0-9]+)#', $value))
{
	$value = str_replace('val', '', $value);
}
build_message($value);
 
0
•••
Regular expression functions can be quite intensive.

PHP:
$value = 'val5';
$value = strstr($value, 'val') ? (int) substr($value, 3) : (int) $value;

I've used the ternary operator just to make it look nicer, and then type juggling to give you an integer value and not a string.
 
0
•••
SecondVersion: THANKS! That's exactly what I was looking for! Although I ended up using something else which I came up with, your expression is really what I wanted to write!

Wildhoney: D-: I don't know what that means... I'm a noob.

I ended up with:
PHP:
.build_message(ltrim($value, "val"))

thanks for the replies!
 
0
•••
Sorry, I probably went too far too quick. You know, all this time in PHP and I completely forgot that ltrim had a second argument! Glad you sorted it.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back