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
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
Link to comment
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.
Link to comment
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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.