RuleBritannia Posted November 2, 2013 Share Posted November 2, 2013 Cant seem to find any solution to this String 1 'Let's all go on holiday : America' String 2 'Lets all go on holiday America' When doing a match for this string, I class this as acceptable, However a straight regex will not accept this, Because of the full colon and comma. What I want to achieve, is exactly a case insensitive search, but with some characters to be insensitive about. To ignore case you just append i to the end of your pattern. I want to pay no attention to ' :, if its there ok, if its not and the rest matchs ok Thanks in advance. Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted November 2, 2013 Author Share Posted November 2, 2013 Update. I just noticed one possible way this "may" be possible, havnt tested yet. What if I was to append a ? to each insensitive character in the string. This would tell regex to either find a , or : and match regardless. PS, If this does work, I would still like to hear your answer if you have alternatives, I believe there may be other ways to achieve this. Thanks all Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted November 2, 2013 Author Share Posted November 2, 2013 Just a little update guys, I tested my above possible solution, and it worked, But I would appreciate some other alternatives as im sure there are some. Quote Link to comment Share on other sites More sharing options...
ignace Posted November 2, 2013 Share Posted November 2, 2013 (edited) What is it exactly that you want to match? What is your current code? $regex = "/Let'?s all go on holiday (: )?America/i"; foreach (array("Let's all go on holiday : America", "Lets all go on holiday America") as $text) { if (preg_match($regex, $text, $matches)) { echo '<div style="color: green">' . $text . '</div>'; } else { echo '<div style="color: red">' . $text . '</div>'; } } Edited November 2, 2013 by ignace Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted November 2, 2013 Author Share Posted November 2, 2013 (edited) What is it exactly that you want to match? What is your current code? $regex = "/Let'?s all go on holiday (: )?America/i"; foreach (array("Let's all go on holiday : America", "Lets all go on holiday America") as $text) { if (preg_match($regex, $text, $matches)) { echo '<div style="color: green">' . $text . '</div>'; } else { echo '<div style="color: red">' . $text . '</div>'; } } The solution I just done works like this $search = 'Rule Britanni'a King of : All'; $ignore[0] = array(',',':'); $ignore[1] = array(',?',':?'); $string = str_replace($ignore[0],$ignore[1],$string); preg_match("/$string/i",$search,$match); var_dump($match); This actually works fine, But wondered if there are other solutions quicker/better then what I have done Edited November 2, 2013 by RuleBritannia 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.