Jessica Posted January 5, 2007 Share Posted January 5, 2007 If I have the following string:$str = "This is a bunch of strings. String One: Test. String Two: Test Two."And I do this:$strings = explode("String", $str);The result is like this:$strings[0] = "This is a bunch of Strings.";$strings[1] = "One: Test.";$strings[2] = "Two: Test Two.";How would I keep the String on the front of the strings which have it. I can't just add String to the front of each line because the first entry does not have String on it.Help? Quote Link to comment https://forums.phpfreaks.com/topic/33016-solved-explode-but-keep-the-string/ Share on other sites More sharing options...
effigy Posted January 5, 2007 Share Posted January 5, 2007 Use PCRE's positive lookahead:[code]<?php $str = "This is a bunch of strings. String One: Test. String Two: Test Two."; $strings = preg_split('/(?=String)/', $str); echo '<pre>', print_r($strings, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33016-solved-explode-but-keep-the-string/#findComment-153768 Share on other sites More sharing options...
Jessica Posted January 5, 2007 Author Share Posted January 5, 2007 Fabulous! :) Quote Link to comment https://forums.phpfreaks.com/topic/33016-solved-explode-but-keep-the-string/#findComment-153776 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.