Jump to content

Archived

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

alpine

add string to string if pattern found

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 ?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
Share on other sites
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  :-\

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites

×

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.