Page 1 of 1

How could I Explode

Posted: Fri Aug 18, 2006 9:31 am
by Supper Baby
I have this string :

Code: Select all

$str ="[am]cgbxgb[/am]
[am]zdgdgd2[/am]
[am]dhgsfdh3[/am]";
I want to explode it to an array :

Code: Select all

$t ="[am]cgbxgb[/am]
[am]zdgdgd2[/am]
[am]dhgsfdh3[/am]";

$str = Explode( '[am]', $t );
        	if ( count($str) > 1 )
        	{
        		foreach ( $str AS $strX )
        		{
        			$str2 = Explode( '[/am]', $strX[1] );
        			$d['a'][] = Array( $str2[0], 0 );
        		}
        	}
I want to put each string between [am][/am] Tag in array.

like [ php ] tage in this forum.

What's the wrong with my code ??

Posted: Fri Aug 18, 2006 9:33 am
by feyd
it's one big string.

Posted: Fri Aug 18, 2006 9:36 am
by Luke
^syntax highlighter points that out.... notice how it's ALL red? WHAPOW! Image

Posted: Fri Aug 18, 2006 9:36 am
by jayshields
There's alot wrong with your code. For starters, you haven't closed your double quotes after you set a string to $str, and you're trying to explode on something which doesn't exist in your string.

Anyway, you will need a regexp pattern for what you are doing. It could probably be done with explode, but it's going to be long winded and a much worse alternative to a regex solution.

I would advise to either read the stickies in the Regular Expressions forum, or wait patiently and hope someone who is half decent at making regexp patterns is feeling generous enough to do it for you.

Posted: Fri Aug 18, 2006 9:41 am
by JayBird
off the top of my head

Code: Select all

$str ="[am]cgbxgb[/am]
[am]zdgdgd2[/am]
[am]dhgsfdh3[/am]"; 

preg_match_all('~\[am\](.*?)\[/am\]~si', $str, $match);

foreach($match[1] as $string)
{
	echo $string."<br />";
}

/* outputs
		cgbxgb
		zdgdgd2
		dhgsfdh3
*/

Posted: Sat Aug 19, 2006 7:00 pm
by Supper Baby
Thank you every body
:lol:
JayBird wrote:off the top of my head

Code: Select all

$str ="[am]cgbxgb[/am]
[am]zdgdgd2[/am]
[am]dhgsfdh3[/am]"; 

preg_match_all('~\[am\](.*?)\[/am\]~si', $str, $match);

foreach($match[1] as $string)
{
	echo $string."<br />";
}

/* outputs
		cgbxgb
		zdgdgd2
		dhgsfdh3
*/


:wink: Smart Idea man

but can you tell me what's the meaning of ~\ Whay we use it ? and when