Author Topic: Match HTML\1.0 or HTML\1.1  (Read 365 times)

0 Members and 1 Guest are viewing this topic.

Offline doa24ukTopic starter

  • Enthusiast
  • Posts: 102
    • View Profile
Match HTML\1.0 or HTML\1.1
« on: March 11, 2010, 05:12:43 PM »
Hey guys,

My two possibilities are to match either HTML\1.0 or HTML\1.1

I was using str_replace to just match one but now I believe I need preg_replace.

Code: [Select]
$responsecode = str_replace("HTTP/1.1", "", $headers[0]);

However I'm completely lost with the regex for this! Help!

Offline brianlange

  • Enthusiast
  • Posts: 226
    • View Profile
Re: Match HTML\1.0 or HTML\1.1
« Reply #1 on: March 11, 2010, 05:49:35 PM »
  $headers = array("HTML\\1.0","HTML\\1.1");
  $pattern = '/HTML\\\1\.[01]/';
  $newHeaders = preg_replace($pattern, "", $headers);

You have to escape the backslash in the subject and then use three backslashes in the pattern.
You have to use a backslash before the period to escape it in the pattern, otherwise the period is treated as a special character, without the backslash it means any single character. The brackets around 0 and 1 mean either character will be matched.

You could also add the start identifier ^ and end identifier $ to the pattern $pattern = '/^HTML\\\1\.[01]$/';
Without these the pattern the above will match strings like HTML\1.123456

« Last Edit: March 11, 2010, 05:57:45 PM by brianlange »