alpine Posted August 5, 2006 Share Posted August 5, 2006 sorry about the title, just couln't find one suitable,here is my problem - in my searchform i want to check for occurances of patterns with dot (.) or commas (,) inbetween letters or numbers where it's no space around them - and include the same pattern to the string only with exchanged comma or dot.Example: 3x2.5 == true2x1,5 == trueYou 2, whatever == false (note the space)hello. How are you == false (also a space)Ive managed to get this far:[code]preg_match_all("/([A-Z0-9]+[,][A-Z0-9])/i", $text, $array)// orpreg_match_all("/([A-Z0-9]+[.][A-Z0-9])/i", $text, $array)[/code]This however produces array[0] and array[1] with the same values.Target is this:$string = "cable 2x2[color=red],[/color]5 cu"; // where 2x2,5 is true$new_string = "cable 2x2[color=red],[/color]5 cu 2x2[color=red].[/color]5";$string = "cable 7x1[color=red].[/color]0 cu"; // where 7x1.0 is true$new_string = "cable 7x1[color=red].[/color]0 cu 7x1[color=red],[/color]0";Any help on this ? Quote Link to comment Share on other sites More sharing options...
alpine Posted August 6, 2006 Author Share Posted August 6, 2006 I realize that my first post was incomplete in the problem-description as it would not work along with the db querys.I've come up with a solution that works, i am however sure it can be done more efficient, maybe in one regex match - so please pinpoint anything that can make it faster (apart from the mysql "LIKE" part though...)[code]function query_dotsandcommas($data) {$new_query = array();if (preg_match("/([0-9]+[,][0-9])/i", $data)){$extra_data = str_replace( "," , "." , $data);$new_query[] = "AND (beskrivelse LIKE '%$data%' OR beskrivelse LIKE '%$extra_data%')";}elseif (preg_match("/([0-9]+[.][0-9])/i", $data)){$extra_data = str_replace( "." , "," , $data);$new_query[] = "AND (beskrivelse LIKE '%$data%' OR beskrivelse LIKE '%$extra_data%')";}else{$new_query[] = "AND beskrivelse LIKE '%$data%'";}$query = implode(' ', $new_query);return $query;}[/code]I also realized that a match inbetween numbers is sufficient Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 6, 2006 Share Posted August 6, 2006 $lookfor = array('/("/([0-9])+(,)([0-9])/i','/([0-9])+(\.)([0-9])/i');$repwith = array('\\1.\\3''\\1,\\3');if (($extra_data = preg_replace($lookfor,$repwith,$data)) === true){ $new_query[] = "AND (beskrivelse LIKE '%$data%' OR beskrivelse LIKE '%$extra_data%')";}else{ $new_query[] = "AND beskrivelse LIKE '%$data%'";}$query = implode(' ', $new_query);return $query;Hopefully something like that should do it. Quote Link to comment Share on other sites More sharing options...
alpine Posted August 6, 2006 Author Share Posted August 6, 2006 Thanks for your reply.I do however get --> Unknown modifier '(' <-- on line --> if (($extra_data = preg_replace($lookfor,$repwith,$data)) === true)Tried to mix with it back and forth - but with no luck :-\ Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 7, 2006 Share Posted August 7, 2006 hmmmaybe you can't cut corners like that then! ;)try this$lookfor = array('/("/([0-9])+(,)([0-9])/i','/([0-9])+(\.)([0-9])/i');$repwith = array('\\1.\\3''\\1,\\3');$extra_data = preg_replace($lookfor,$repwith,$data));if (strcomp($extra_data,$data) != 0){ $new_query[] = "AND (beskrivelse LIKE '%$data%' OR beskrivelse LIKE '%$extra_data%')";}else{ $new_query[] = "AND beskrivelse LIKE '%$data%'";}$query = implode(' ', $new_query);return $query; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.