Jump to content

help with split()


a.d.b.

Recommended Posts

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, thanks

Audun

Link to comment
https://forums.phpfreaks.com/topic/29293-help-with-split/
Share on other sites

thanks for the quick reply  :)

however, when I used the

split(";;\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
Link to comment
https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134266
Share on other sites

well you could use
split(";;.*;", $data1);
it would work but it's way greedy

if your only going to put links in between them then use a URLregex
I 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]
Link to comment
https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134767
Share on other sites

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 array
preg_match("/(.*?);;(.*?);(.*)/ms", $data1, $matches);

// Delete the first element in the array (the full match)
array_shift($matches);

// Dump the data to show what was captured
echo "<pre>\n";
var_dump($matches);
echo "</pre>\n";
?>[/code]

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/29293-help-with-split/#findComment-134780
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.