Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_split or other solution please

Status
Not open for further replies.

elck

Programmer
Apr 19, 2004
176
NL
Hi there,

Suppose I have a text like this:

$a="Hi there, suppose [I have:a text] like this, [can:you] help me?";

Now I would like to have an array consisting of:

$b[0] : "Hi there, suppose ";
$b[1] : "~I have";
$b[2] : "~~a text";
$b[3] : " like this, ";
$b[4] : "~can";
$b[5] : "~~you";
$b[6] : " help me?";

Where the ~ help me find which elements were found between the square brackets.


I tried preg_split, but what would the pattern look like?

Thanks,

Elck
 
I found a way all by myself! ;-)
But I doubt it will get a beauty award!
Any improvements welcome!

Code:
$i=0;
$b=preg_split("/(\[|:|\])/",$a,-1,PREG_SPLIT_DELIM_CAPTURE);

foreach ($b as $e) {
	switch ($e) {
		case '[' :
			$b[$i+1] = "~" . $b[$i+1];
			$b[$i]="";
			break;
		case ':' :
			$b[$i+1] = "~~" . $b[$i+1];
			$b[$i]="";
			break;
		case ']' :
			$b[$i]="";
}

$i++;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top