Jump to content

split string


canadabeeau

Recommended Posts

Hi Guys,

 

I need a PHP function which will place into and array all text that does not sit between { or }, eg

#header_crm a {

display:block;

height:260px;

margin:0px;

padding:0px;

}

 

it would add to the array "#header_crm a" only and not the other content, any ideas. I have tried explode("{", $file_contents); but it still displays the other stuff even if I use "{,}"

 

As usual thanks for all the help in advance

Link to comment
Share on other sites

I am using this code, but the problem is there is multiple cases of text that lies between { and } and this code only does one instance

function ExtractString($str, $start, $end)
{
	$str_low = strtolower($str);
	$pos_start = strpos($str_low, $start);
	$pos_end = strpos($str_low, $end, ($pos_start + strlen($start)));
	if ( ($pos_start !== false) && ($pos_end !== false) )
	{
		$pos1 = $pos_start + strlen($start);
		$pos2 = $pos_end - $pos1;
		return substr($str, $pos1, $pos2);
	}
}

$array = ExtractString($file_contents, '{', '}');
echo $array;

Link to comment
Share on other sites

Please in future, if you want anyone to help, be has helpful to us as possible.  Even after being specifically asked for more details you still didn't say what you had tried (so we could fix it) nor did you pick it up and run when given a very strong hint towards an easy solution.

 

You can split up the CSS selectors and declarations using strtok (looking for curly braces) like:

 

$tok = strtok($subject, "{}");
while ($tok !== FALSE)
{
$toks[] = $tok;
$tok = strtok("{}");
}

 

That will simply give a long list of selector 1, declarations 1selector n, declarations n. If you want the parts in a different array structure (or, however you want them!) then you can change the tokenising process accordingly or work on that array, whichever suits your needs.

 

Since this is in the regex forum here's a regex which also splits up the different parts:

 

preg_match_all('/([^{]+){([^}]+)}/', $subject, $matches, PREG_SET_ORDER);

 

Go wild.  :shy:

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.