Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip IP address from string 1

Status
Not open for further replies.
Aug 27, 2001
502
US
I will be receiving a string containing an IP address and domain name.

Example:
mydomain.com[192.168.245.13]

I need to strip the IP address out and add it to a variable. In other words, I need to keep the IP address and nothing else.

The construction of the string will always contain the IP address within square brackets.

I'm looking for the most efficient way to do this.

Any suggestions?

Thanks,
Ron

“If you are irritated by every rub, how will you be polished?” ~ Mevlana Rumi


Do you live in Michigan? Join us in the Tek-Tips in Michigan forum.
 
The script:

Code:
<?php

$a = 'mydomain.com[192.168.245.13]';

$begin = strpos ($a, '[');
$end = strpos ($a, ']');

print substr($a, $begin + 1, $end - $begin - 1);
?>

outputs:

[tt]192.168.245.13[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
Perfect!!

I had a brainfart. I should have been able to figure that one out myself. I was trying to use regular expressions. BLAH!

Thanks!

Ron

“If you are irritated by every rub, how will you be polished?” ~ Mevlana Rumi


Do you live in Michigan? Join us in the Tek-Tips in Michigan forum.
 
Regular expressions were my first thought, too. Then I got to the part of your post which reads, "I'm looking for the most efficient way to do this." The PHP online manual has multiple NOTEs to not use regexes unless necessary.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top