-2

I have a string like this:

"(01:42 PM)76.124.231.190 William: Hello?"

I need a regex to remove the IP address like this:

preg_replace($regex, "", "(01:42 PM)76.124.231.190 William: Hello?")
// --> "(01:42 PM) William: Hello?"

I've seen this: Regex to match an IP address, but that checks if the string is an IP, not if it contains it.

Also, it don't have to be perfect, 999.99.999.999 is ok for simplicity.

Community
  • 1
  • 1

3 Answers3

0

If you are going to have the same string length before the IP each time you could just use a substring in php and select out the ip address. Just use the substr

http://www.php.net/manual/en/function.substr.php

patrick9382
  • 97
  • 1
  • 11
0

At it's simplest, this regex should work.

preg_replace('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', "", "(01:42 PM)76.124.231.190 William: Hello?")
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
  • for good IP regex > http://stackoverflow.com/questions/9165922/regex-for-ip-address/20270082#20270082 – Alban Jul 28 '14 at 16:16
0

It seems as if you are trying to pull out a specific IP out of a string. You don't need regex for that.

This can be done easily with str_replace:

$myStringWithIP = '(01:42 PM)76.124.231.190 William: Hello?';
str_replace('76.124.231.190', '', $myStringWithIP);

#$myStringWithIP now equals '(01:42 PM) William: Hello?'

I hope this helps!

HelpingHand
  • 1,045
  • 11
  • 26
  • Yes, but str_replace won't remove anything in the string that looks like an IP. Only the one you specify in the code. – HelpingHand Jun 04 '14 at 16:11
  • And that's what I don't want. –  Jun 04 '14 at 16:14
  • You are very belligerent. In your question you said: `I need a regex to remove the IP address like this:` That isn't true. The IP address can be removed using `str_replace`. – HelpingHand Jun 04 '14 at 16:18
  • Maybe you should take a look at the title then. –  Jun 04 '14 at 16:19