To check for a products size i need to search through a simple string like: 32sdfsjkndgfjkdgndkj24020ldsfsfsd
To find the value inside the string i'm using strpos to look for the different sizes.
My current code is:
if (strpos($size, "1875cl") || strpos($size, "01875l")) {
$size = '18,75cl';
} elseif (strpos($size, "20cl") || strpos($size, "020l") || strpos($size, "02l")) {
$size = '20cl';
}
The problem is when i use an if statement.
The first if is working great. Then when the elseif is checking for another value inside the string - also with strpos the scripts returns a fatal error saying that the function is undefined:
Fatal error: Call to undefined function strpos() in .....
This works, same as yours will work, the only possible way it would break is that the data is overwritten or there is wrong data in the $size
variable.
You can sanitize the $size
variable more by using preg_replace( '/[^a-zA-Z0-9]/', '', $size );
and if you use the code below you can save the $correctedSize
variable with the name.
$prodNames = array( '\x0D\x0D\x0D \n\rkajshdfsajkdhaksjd1875cladsas', '۞ ۩ εїз Ƹ̵̡Ӝ̵̨̄kajshdfsajkdhaksjd1875cladsas', 'kajshdfsajkdhaksjd20clcladsas','02lkajshdfsajkdhaksjdadsas', 'kajshdfsajkdhaksjd1875cladsas', 'kajshdfsajkdhaksjd1875cladsas', 'kajshdfsajkdhaksjd20clcladsas','02lkajshdfsajkdhaksjdadsas' );
foreach ( $prodNames as $prodNameRaw ) {
$correctedSize = 0;
$prodName = preg_replace( '/[^a-zA-Z0-9]/', '', $prodNameRaw );
if ( strstr( $prodName, '1875cl' ) || strstr( $prodName, '01875l' ) ) {
$correctedSize = '18,75cl';
} elseif ( strstr( $prodName, '20cl' ) || strstr( $prodName, '020l' ) || strstr( $prodName, '02l' ) ) {
$correctedSize = '20cl';
}
}