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
https://forums.phpfreaks.com/topic/193199-split-string/
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
https://forums.phpfreaks.com/topic/193199-split-string/#findComment-1017358
Share on other sites

My aim is to have a PHP script read and extract data from the CSS and place into a DB, so does anyone have an idea or solution, the strtok breaks it up into little pieces so I dont think that really helps, unless you can suggest a reliable way to stich it back together

 

Thanks again

Link to comment
https://forums.phpfreaks.com/topic/193199-split-string/#findComment-1017446
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
https://forums.phpfreaks.com/topic/193199-split-string/#findComment-1018937
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.