a.d.b. Posted December 3, 2006 Share Posted December 3, 2006 I have tried to search the forums, but no luck. ???I am trying to devide a text by using the php split function. This is all good, except from the fact that I need to split it by ";;VARIABLETEXT;", meaning I need to split it by double semicolons followed by a text that varies followed by another semicolon.I am trying with split( ";;%;" , $data1 );but because of my lack of php-experience, I am basically stuck. Any easy solution to this problem? I have also tried with the explode() function and the preg_split().Please, help a php newbie out, thanksAudun Quote Link to comment https://forums.phpfreaks.com/topic/29293-help-with-split/ Share on other sites More sharing options...
Zane Posted December 3, 2006 Share Posted December 3, 2006 split(";;\w*;", $data1); Quote Link to comment https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134265 Share on other sites More sharing options...
a.d.b. Posted December 3, 2006 Author Share Posted December 3, 2006 thanks for the quick reply :)however, when I used thesplit(";;\w*;" , $data1 );it split the text where "[b];;;[/b]" was found (as I would expect, and need to happen :), because it sometimes is no text between ;; and ; ), but it didn't split the text where ";;somevariabletext;". I do not know if it is relevant, but the variable text is in fact an url, like this: "[b];;[/b]http://www.variable.com/variblename.variableextention[b];[/b]".an example of the text would be "audun is doing his homework[b];;[/b]http://www.audun.com/test.php[b];[/b]whenever he feels like it"Am I doing it all wrong? Thanks,Audun Quote Link to comment https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134266 Share on other sites More sharing options...
Zane Posted December 4, 2006 Share Posted December 4, 2006 well you could usesplit(";;.*;", $data1);it would work but it's way greedyif your only going to put links in between them then use a URLregexI got this from http://www.osix.net/modules/article/?id=586[code]$regex = "(https?://)" . "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //user@ . "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP- 199.194.52.184 . "|" // allows either IP or domain . "([0-9a-z_!~*'()-]+\.)*" // tertiary domain(s)- www. . "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // second level domain . "[a-z]{2,6})" // first level domain- .com or .museum . "(:[0-9]{1,4})?" // port number- :80 . "((/?)|" // a slash isn't required if there is no file name . "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)";split(";;" . $regex . ";", $data1);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134767 Share on other sites More sharing options...
HuggieBear Posted December 4, 2006 Share Posted December 4, 2006 Why not give this a try, it's using preg_match().[code]<?php// Test data as provided$data1 = "audun is doing his homework;;http://www.audun.com/test.php;whenever he feels like it";// Match the data and stick it into the matches arraypreg_match("/(.*?);;(.*?);(.*)/ms", $data1, $matches);// Delete the first element in the array (the full match)array_shift($matches);// Dump the data to show what was capturedecho "<pre>\n";var_dump($matches);echo "</pre>\n";?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134780 Share on other sites More sharing options...
Nicklas Posted December 4, 2006 Share Posted December 4, 2006 ex[CODE]<?php$str = "audun is doing his homework;;http://www.audun.com/test.php;whenever he feels like it";$array = preg_split('/;+/', $str);print_r($array);?>[/CODE] Quote Link to comment https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134954 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.