I have a variable who's value is CSPG3_PistolGrip_3inch-x-15.00-x-14.00-x-13.00-x-12.00

the "-x-" delimits prices so I've used the following code to break them up:

if(preg_match('||',$key)) {

		$parts = explode('-x-',$productdetails);

		for($i = 0; $i < count($parts); $i++) // we start at zero
		{
			$prices[] = strtr($parts[$i],'_',' '); // and clean the underscore

		}

I need to have a 2nd pre_match function inside this that basically says if the variable starts with "CSPG" then do something. Would anybody know what code would I use and where would I put it?

    you wont even need to use the regex engine for that. something as simple as
    if (substr($productdetails, 0, 4) == "CSPG") {

      If you need to be more generic, then use strpos. For example, if you need to check for the abbreviation at the beginning of the word, but it could be more or less than 4 characters, do this:

      $needle = "CSPG"; // substitute any string you are looking for
      if (strpos($productdetails, $needle) === 0) { 
          do something;
      }

      You must use === because strpos will return false if it can't find the string, and 0 if it finds it at the beginning of your string. If you use ==, then when strpos returns FALSE, the condition will evaluate as true, and "do something" will happen even though it didn't find a match.

        Write a Reply...