WeirdMystery Posted May 1, 2010 Share Posted May 1, 2010 Hi, I am trying to insert "| " before words, so I can use the PHP explode function on them. Example: "1 weirdmystery 36668 89 2 hdlowrider 1042 11 3 ds_blockcommunity 301 5 4 xredx22 141 4 5 nitt_from_bcy 94 2 6 noobwithagun 47 1" would turn into this "1 | weirdmystery 36668 89 2 | hdlowrider 1042 11 3 | ds_blockcommunity 301 5 4 | xredx22 141 4 5 | nitt_from_bcy 94 2 6 | noobwithagun 47 1". I tried str_replace but that doesn't have a position parameter so I don't replace the word or letter itself. Must be something with preg_replace and some regrex I don't know of. Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/ Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 Use preg_replace preg_replace('#(\d)\s([^\d])#', '$1 | $2', $str); Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051672 Share on other sites More sharing options...
WeirdMystery Posted May 1, 2010 Author Share Posted May 1, 2010 Use preg_replace preg_replace('#(\d)\s([^\d])#', '$1 | $2', $str); That inserts | before numbers too. Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051674 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 Does not. Sat May 1 19:28:19 EDT 2010 [7 files, 3.4MB] ~/Desktop [eureka@dc Desktop]$ php test.php 1 | weirdmystery 36668 89 2 | hdlowrider 1042 11 3 | ds_blockcommunity 301 5 4 | xredx22 141 4 5 | nitt_from_bcy 94 2 6 | noobwithagun 47 1 Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051679 Share on other sites More sharing options...
WeirdMystery Posted May 1, 2010 Author Share Posted May 1, 2010 Does not. Sat May 1 19:28:19 EDT 2010 [7 files, 3.4MB] ~/Desktop [eureka@dc Desktop]$ php test.php 1 | weirdmystery 36668 89 2 | hdlowrider 1042 11 3 | ds_blockcommunity 301 5 4 | xredx22 141 4 5 | nitt_from_bcy 94 2 6 | noobwithagun 47 1 That regrex returns "1 | weirdmystery 37179 | 90 | 2 | hdlowrider 1042 | 11 | 3 | ds_blockcommunity 301 | 5 | 4 | xredx22 | 141 | 4 | 5 | nitt_from_bcy 94 | 2 | 6 | noobwithagun 47 | 1 |" Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051680 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 o.O Post the code you used. Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051682 Share on other sites More sharing options...
WeirdMystery Posted May 1, 2010 Author Share Posted May 1, 2010 o.O Post the code you used. $content_lower = strtolower($parsed); $strip_tags = strip_tags($content_lower); $remove_tabs = str_replace(" ", "", $strip_tags); $remove_linebreaks = str_replace(array("\r", "\r\n", "\n"), "", $remove_tabs); $remove_before_first_number = preg_replace('#^([^\d]+)?#','', $remove_linebreaks); //Your solution... $final = preg_replace('#(\d)\s([^\d])#', '$1 | $2', $remove_before_first_number); Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051684 Share on other sites More sharing options...
WeirdMystery Posted May 1, 2010 Author Share Posted May 1, 2010 o.O Post the code you used. $content_lower = strtolower($parsed); $strip_tags = strip_tags($content_lower); $remove_tabs = str_replace(" ", "", $strip_tags); $remove_linebreaks = str_replace(array("\r", "\r\n", "\n"), "", $remove_tabs); $remove_before_first_number = preg_replace('#^([^\d]+)?#','', $remove_linebreaks); //Your solution... $final = preg_replace('#(\d)\s([^\d])#', '$1 | $2', $remove_before_first_number); Sorry, must have been the Forum's formatting which removed the extra spaces... Now thats the string that I was trying to modify. 1 weirdmystery 37179 90 2 hdlowrider 1042 11 3 ds_blockcommunity 301 5 4 xredx22 141 4 5 nitt_from_bcy 94 2 6 noobwithagun 47 1 Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051686 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 And you couldn't figure out how to change my regex to accommodate that? $final = preg_replace('#(\d)\s+([^\s\d])#', '$1 | $2', $remove_before_first_number); Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051690 Share on other sites More sharing options...
WeirdMystery Posted May 1, 2010 Author Share Posted May 1, 2010 And you couldn't figure out how to change my regex to accommodate that? $final = preg_replace('#(\d)\s+([^\s\d])#', '$1 | $2', $remove_before_first_number); Sorry, I have no knowledge about regrexes... Just to confirm that, I did not know regrex was spelled regex. Oops, forgot to say thanks!... Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051691 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 It's cool. Glad it works. =] Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051693 Share on other sites More sharing options...
ChemicalBliss Posted May 2, 2010 Share Posted May 2, 2010 You could just create the array using preg_match_all: <?php $str = "1 weirdmystery 37179 90 2 hdlowrider 1042 11 3 ds_blockcommunity 301 5 4 xredx22 141 4 5 nitt_from_bcy 94 2 6 noobwithagun 47 1"; // No need to explode, use preg match to create the array you need. $final = preg_match_all('/(\d+)\s+([a-z0-9_]+)\s+(\d+)\s+(\d+)/i', $str, $matches); // Then you can loop the results like so; for($i=0;$i<count($matches[0]);$i++){ echo(' Player ID: '.$matches[1][$i].' <br /> Player Name: '.$matches[2][$i].' <br /> Player Score?: '.$matches[3][$i].' <br /> Player Team Points?: '.$matches[4][$i].' <br /> <br /> '); } ?> -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051698 Share on other sites More sharing options...
WeirdMystery Posted May 2, 2010 Author Share Posted May 2, 2010 You could just create the array using preg_match_all: <?php $str = "1 weirdmystery 37179 90 2 hdlowrider 1042 11 3 ds_blockcommunity 301 5 4 xredx22 141 4 5 nitt_from_bcy 94 2 6 noobwithagun 47 1"; // No need to explode, use preg match to create the array you need. $final = preg_match_all('/(\d+)\s+([a-z0-9_]+)\s+(\d+)\s+(\d+)/i', $str, $matches); // Then you can loop the results like so; for($i=0;$i<count($matches[0]);$i++){ echo(' Player ID: '.$matches[1][$i].' <br /> Player Name: '.$matches[2][$i].' <br /> Player Score?: '.$matches[3][$i].' <br /> Player Team Points?: '.$matches[4][$i].' <br /> <br /> '); } ?> -cb- Thanks man, thats even a better solution... Sorry guys for not telling you guys that the number before the username is the user id. (BTW, its a Folding@home statistics lister for our team.) Quote Link to comment https://forums.phpfreaks.com/topic/200406-insert-character-before-words/#findComment-1051703 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.