PHP MySQL Query IP As ****

I have a form my users fill out that write to a MySQL DB. It also captures their IP and places it in a field called IP.

When I query this information into an html page how can I display the results as ****.****.****.XXXX
I only want the last octect of the address to display on the page, but I want the MySQL to record the actual IP address.

Is there a way I can make this happen?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Try this.  Replace the dummy names with your real variables.
$display_string = "****.****.****.".substr(IPstring, -4, 4);

Open in new window

mimoserAuthor Commented:
How would I add that in relation to

$sql = "SELECT *, DATE_FORMAT(`date`,'%a, %b %d %Y %h:%i %p') AS showdate FROM Request";
$result = mysql_query($sql);

<font color="lime"><?php  echo $row['IP']; ?></font>

Sorry, I'm still a newbi to this lol

There is a simple way to do that:

<input type="password" name="1st_byte" value="<?php echo $1st_octet; ?>">
<input type="password" name="2nd_byte" value="<?php echo $2nd__octet; ?>">
<input type="password" name="3rd_byte" value="<?php echo $3rd_octet; ?>">
<input type="text" name="last_byte" value="<?php echo $last_octet; ?>">

OR you can simply add only an input text

<input type="text" name="IP" value="****.****.****.<?php echo $1st_octet; ?>">

Good  luck,
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

$1st_octet in the last input is actually the last octet.
mimoserAuthor Commented:
@Ultra Master
I already have the form that works great adding the full IP to a DB feild called 'IP'

I'm trying to now show it on an html page like
<font color="lime"><?php  echo $row['IP']; ?></font>

Which works and shows all 4 octet's, but I was trying to make it only show the last octet.
Shinesh PremrajanEngineering ManagerCommented:
<font color="lime"><?php  echo substr($row['IP'],strrpos($row['IP'],"."),strlen($row['IP'])); ?></font>

Hope this helps
mimoserAuthor Commented:
@ shinuq
This is Working!!
This is very, very close, and I may end up taking it unless there is a way to make it say something like "x" or ** out the other characters leaving the last octet? such as  or  ***.***.***.255  or something

If not, thats ok, this comes the closest
Shinesh PremrajanEngineering ManagerCommented:
I think you must be looking at this.

<font color="lime"><?php  echo preg_replace("/\d/","X",substr($row['IP'],0,strrpos($row['IP'],"."))).".".substr($row['IP'],strrpos($row['IP'],"."),strlen($row['IP']));?></font>


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mimoserAuthor Commented:
Perfect!! Can you please explain this for me?
mimoserAuthor Commented:
This works great, can you please explain it a little more to me about whas going on?
Shinesh PremrajanEngineering ManagerCommented:

1) replace the occurrence of the any decimal into a X character using the preg_replace function.
2) as we need to do the replacement of the first three octets we will be extracting the first three octets using the sub string  and the strrpos reverse traversal function.


3) get the fourth octet from the string using the substr and the strrpos functionality.


Hope this clarifies
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.