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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.