php-n00b Posted December 15, 2009 Share Posted December 15, 2009 What's Up? OK, So since the need for php has been cropping up in my latest project a lot lately, I decided to get down and get learning! Somehow, I even amazed myself, I got this far. Basically when you use a basic IP php logger, It logs your IP and date to separate lines, So if your getting lots of visits then your gonna have a bulky list, Lets see if we can change that? <?php $ip = $_SERVER['REMOTE_ADDR']; $datum = date("d-m-y / h:a"); $invoegen = "$datum - $ip \n"; $fopen = fopen("Database.html", "a"); $echo = file_get_contents('Database.html'); $old = "$ip"; $new = "\n"; $make = str_replace($old, $new, $echo); fwrite($fopen, $make); fwrite($fopen, $invoegen); fclose($fopen); # ip logging completed! ?> As you can see it gets your IP, And then attempts to replace it in the log, And it works 100% fine, But now its time for the harder part, The date, How could we remove the date that the IP came with, str_replace doesn't use wildcards does it? (*)? Thanks Bye. Link to comment https://forums.phpfreaks.com/topic/185274-str_replace/ Share on other sites More sharing options...
nafetski Posted December 16, 2009 Share Posted December 16, 2009 Nope, str_replace doesn't handle an * type character... ...however Regular Expressions do! What do you want to replace exactly? If your dates are formatted like... 12-12-2009 (or) 12/12/2009... A regex like this would match. \d{2}(\-|\/)\d{2}(\-|\/)\d{4} To break it down... \d means any digit. {2} means 2 of them (\-|\/) looks confusing, but broken down it's not too bad. The backslashes just escape the special chracters, in this area we are looking for a hyphen OR a backslash. Then pretty much repeating that across. So an example of how to replace it would be (untested, but should work) <?php $pattern = "/\d{2}(\-|\/)\d{2}(\-|\/)\d{4}/" $string = "This is a string that has a bunch of stuff and a date 12-01-2009 in it"; $newstring = preg_replace($pattern,"MONKEYS KNOW KUNG FU",$string); echo $newstring; ?> This should echo out.. This is a string that has a bunch of stuff and a date MONKEYS KNOW KUNG FU it Regex is intimidating at first, but once you get the hang of it - makes string manipulation way easy Good luck, and let me know if you need any more help. Link to comment https://forums.phpfreaks.com/topic/185274-str_replace/#findComment-978315 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.