Grab number left and right of string using PHP for MYSQL -


i have mysql database table column has size value in multiple formats users manually entered different value formats.

using php need iterate db table , process field grab withd , height value each column when column value matches pattern create...

below 90% of values in these formats. many same format either single or double digit left or right side of lowercase or capital x

usung php how match each string strip non numeric characters value on left , right side of x.

left = width
right side = height

1x1 1x1 1"x1"  12x12 12x12 12"x12" 12"x12"  null '' ,_ empty field 

i need these values width , height variable in php.

if can grab left of lowercase , capital x ad right of , strip non numbers think work easily


there other values , 1 should ignored not fit pattern. below example of of odd values found far...

18" channel letters  64x20 x 2  glass dimensions: 12"x72"  172.61 cm x 28.46 cm 230.15 cm x 42.07 cm 24x24 interior double sided 

these type of values should ignored can manually edit these later

i've written function called rough_strip_all should strip characters in string except listed. adding step may resolve issue you, if doesn't, might have recompiling enable utf8 support pcre.

<?php  // strips out characters except in allowed set function rough_strip_all( $string, $allowed_set = '0123456789x. ' ) {     // takes allowed set, splits character character,     // converts each character in array ascii value     $allowed_ascii = array_map( function($a) {         return ord( $a );     }, str_split( $allowed_set ) );      $return = '';     for( $i = 0, $ilen = mb_strlen( $string ); $i < $ilen; $i++ )     {         // check if ascii value of current character in list of allowed         // ascii characters given. if is, add return string         $ascii = ord( $string{$i} );         if( in_array( $ascii, $allowed_ascii ) )         {             $return .= $string{$i};         }     }      // returns newly compiled string     return $return; }  // original string $string = "misc text: 35.25”x 21.00” 123 text 456";  // display original string echo "original string: {$string}<br />";  // strips out characters except following: '0123456789x. ' $string = rough_strip_all( strtolower( $string ) );  // strip out characters except numbers, letter x, decimal points, , spaces $string = preg_replace( '/([^0-9x \.])/ui', '', $string );  // find fits number x number format (including decimal numbers) preg_match( '/([0-9]+(\.[0-9]+)?) ?x ?([0-9]+(\.[0-9]+)?)/ui', $string, $values );  // match found if( !empty( $values ) ) {     // set dimensions in easy read variables     $dimension_a = $values[1];     $dimension_b = $values[3];      // values returned     echo "dimension a: {$dimension_a}<br />";     echo "dimension b: {$dimension_b}<br />"; } // no match found else {     echo "no match found."; }  ?> 

this should should work additional outliers added in strips out non-essential characters first, attempts make match. i've added display logic can see original string , each dimension after been processed, or message if there has been no match.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -