Jump to content


Photo

add string to string if pattern found


  • Please log in to reply
4 replies to this topic

#1 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 05 August 2006 - 09:57 PM

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 == true
2x1,5 == true
You 2, whatever == false (note the space)
hello. How are you == false (also a space)

Ive managed to get this far:
preg_match_all("/([A-Z0-9]+[,][A-Z0-9])/i", $text, $array)
// or
preg_match_all("/([A-Z0-9]+[.][A-Z0-9])/i", $text, $array)
This however produces array[0] and array[1] with the same values.

Target is this:

$string = "cable 2x2,5 cu"; // where 2x2,5 is true

$new_string = "cable 2x2,5 cu 2x2.5";

$string = "cable 7x1.0 cu"; // where 7x1.0 is true

$new_string = "cable 7x1.0 cu 7x1,0";


Any help on this ?

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 06 August 2006 - 05:01 AM

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...)

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;
}

I also realized that a match inbetween numbers is sufficient

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 06 August 2006 - 10:14 PM

$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.
follow me on twitter @PHPsycho

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 06 August 2006 - 11:23 PM

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  :-\

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 07 August 2006 - 12:30 AM

hmm
maybe 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;


follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users