...

View Full Version : finding second to last in a string?



jasonc310771
02-17-2008, 11:28 AM
i am wanting to use just the first two numbers of the IP address.

how do i find the second '.' in a string?

thanks

i.e.

'192.168.0.1'

and have returned...

'192.168'

oesxyl
02-17-2008, 12:23 PM
$oldip = '192.168.0.1';
$newip = preg_replace("/^(\d{1-3}\.\d{1-3})\.\d{1-3}\.\d{1-3}$/","$1",$oldip);


not tested.

or use strpos, with limit and extract first part.

best regards

Rohan_Shenoy
02-17-2008, 12:51 PM
Simple method is to use explode() (http://www.w3schools.com/php/func_string_explode.asp) functions


<?php
$ipAddress="192.168.0.1";
$array=explode(".",$ipAddress);
echo "$array[0].$array[1]";//will return 192.168
?>

jasonc310771
02-17-2008, 01:33 PM
wishing to check a fields first characters match a given string.

say lastloginip = "192.168.0.1"

wishing to check the first part of lastloginip string, up to the length of $iptocheck string.

please advise how i can do this using the following mysql statement.

thanks




$array=explode(".",$ip);
$iptocheckfor "$array[0].$array[1]";
$res = @mysql_query("UPDATE `users` SET `lastlogindatetime` = '".$datetimenow."' WHERE `lastloginip` = '".$iptocheckfor."' LIMIT 1") or die(mysql_error());



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum