How could I Explode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Supper Baby
Forum Commoner
Posts: 27
Joined: Tue Aug 01, 2006 11:33 pm

How could I Explode

Post 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 ??
Last edited by Supper Baby on Fri Aug 18, 2006 11:24 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's one big string.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

^syntax highlighter points that out.... notice how it's ALL red? WHAPOW! Image
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
*/
Supper Baby
Forum Commoner
Posts: 27
Joined: Tue Aug 01, 2006 11:33 pm

Post 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
Post Reply