odonel Posted July 26, 2012 Share Posted July 26, 2012 Hello I have a text file with many lines of text in this format. Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt I'd like to break them like this (date|~|username|~|message) so that I can insert each line in mysql the delimiter I want to add is |~| This is my first attempt to break the strings first, but got lost in the regex $string1="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt"; preg_match_all('/(\w{3}\s\w{3}\s\d{2}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\d{4},)|(\w{1,15}.*\()-/', $string1, $match, PREG_PATTERN_ORDER); Quote Link to comment https://forums.phpfreaks.com/topic/266278-add-delimiter-to-regular-expression/ Share on other sites More sharing options...
jazzman1 Posted July 26, 2012 Share Posted July 26, 2012 Do you want to be something like this: http://php.net/manual/en/function.preg-replace.php $string="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt"; $patterns = array(); $patterns[0] = '/\,/'; $patterns[1] = '/(?<=all)\:/'; $replacements = array(); $replacements[1] = '~'; $replacements[0] = '~'; echo preg_replace($patterns, $replacements, $string); Quote Link to comment https://forums.phpfreaks.com/topic/266278-add-delimiter-to-regular-expression/#findComment-1364579 Share on other sites More sharing options...
odonel Posted July 26, 2012 Author Share Posted July 26, 2012 Thanx jazz, with your help, I added 2 patterns, but could not get the IP address by itself $string="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt"; $patterns = array(); $patterns[0] = '/\,/'; $patterns[1] = '/(\(.*\))/'; $patterns[2] = '/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/'; $patterns[3] = '/(?<=all)\:/'; $replacements = array(); $replacements[0] = '!~'; $replacements[1] = '!~'; $replacements[2] = '!~'; $replacements[3] = '!~'; echo preg_replace($patterns, $replacements, $string); Quote Link to comment https://forums.phpfreaks.com/topic/266278-add-delimiter-to-regular-expression/#findComment-1364646 Share on other sites More sharing options...
.josh Posted July 26, 2012 Share Posted July 26, 2012 $string="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt"; preg_match('~([^,]+),\s*([^(]+)\(([^)]+)\)[^:]+.*)~',$string,$parts); array_shift($parts); // unset($parts[2]); // uncomment this line if you do NOT want IP address included $string = implode('|~|',$parts); echo $string; Output: Wed Jul 04 08:23:55 EDT 2012|~|johndoe|~|192.168.0.190|~| testeeeeeeeeeeeeeeeeettttttttttttttttttttt Quote Link to comment https://forums.phpfreaks.com/topic/266278-add-delimiter-to-regular-expression/#findComment-1364660 Share on other sites More sharing options...
.josh Posted July 26, 2012 Share Posted July 26, 2012 p.s. - storing delimited values in a single cell in a db table is generally a bad idea. It will make it a lot harder for you later on if you want to write something that queries the db looking for individual values. I highly recommend you store your data in separate columns instead of a single column. Quote Link to comment https://forums.phpfreaks.com/topic/266278-add-delimiter-to-regular-expression/#findComment-1364664 Share on other sites More sharing options...
jazzman1 Posted July 26, 2012 Share Posted July 26, 2012 Thanx jazz, with your help, I added 2 patterns, but could not get the IP address by itself You need to learn a little of RegEx and how it works in the string... Quote Link to comment https://forums.phpfreaks.com/topic/266278-add-delimiter-to-regular-expression/#findComment-1364713 Share on other sites More sharing options...
odonel Posted July 26, 2012 Author Share Posted July 26, 2012 Josh's solution is perfect jazz, I will work on the second part of getting the arrays into the mysql tables. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/266278-add-delimiter-to-regular-expression/#findComment-1364737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.