Jump to content

Big Problem


RuleBritannia

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by ignace
Link to comment
Share on other sites

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 by RuleBritannia
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.