Jump to content

add string to string if pattern found


alpine

Recommended Posts

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:
[code]
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)
[/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 ?
Link to comment
https://forums.phpfreaks.com/topic/16664-add-string-to-string-if-pattern-found/
Share on other sites

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
$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.
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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.