Jump to content

Match Regex in String and Return ALL as Array


maxudaskin

Recommended Posts

Hey guys,

I have some code in which I am trying to find [ code ]*[ / code ], explode the entire string into the segments, and return a flag for the matched part.

 

Turn this: Some random content. [ code ]run main;[ / code ] That was the main program.[ code ]include 'random.php';[ / code ]

into this: [0]Some random content. [1]run main; [2] That was the main program. [3]include 'random.php'

With flags: [0]1 [1]3

So what's the code, and what's wrong with it that you're not getting the desired results?

 

What I have uses preg_match_all. It runs the code first then outputs the rest. I can't seem to figure out how to do it the other way.

function parseInlineCode($string = NULL, $body = false)
{
	$regex = "/\[code\](.+)\[\/code\]/i";
	preg_match_all($regex, $string, $code);
	print_r($code);
	/*eval('?>' . $code[1][0] . '<?php');*/
	$code  = explode(";", $code[2]);

	for($i = 0; $i < count($code); $i++) {
		$this->runCode($code[$i], $body);
	}
}

 

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.