xxBUCKSxx Posted September 25, 2008 Share Posted September 25, 2008 Hi all, Apologies for being completely lame!! help would be greatly appreciated coz i'm confused :-\ I have a variable that looks like this $ptext =" lots of interesting text [store]http://www.p4tv-video.com[/store] [dir]q2 flash[/dir] [file]q2_01_aaron_town_v_ben_evans.flv[/file] [vid]video.flv[/vid] " i want to remove all the tags out of their and split them into variables so $store = "http://www.p4tv-video.com" $dir = "q2 flash" $file = "q2_01_aaron_town_v_ben_evans.flv" $vid = "video.flv" so that all is left in the $ptest variable is:- $ptext =" lots of interesting text" i hope I've explained that properyly, if it's a lame question then sory, I've been checkin out the php manual online and i'm just getting lost i'm thinking there must be a simple way, your help would save my life:) many thanks Steve ' xxBUCKSxx' Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/ Share on other sites More sharing options...
DarkWater Posted September 25, 2008 Share Posted September 25, 2008 You'll need to use regular expressions for this. Are those 4 pieces of data always right on newlines like that? Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650761 Share on other sites More sharing options...
xxBUCKSxx Posted September 25, 2008 Author Share Posted September 25, 2008 what it is for I'm using smf for a back end to hold all the data fo a website but need to add variables inside a forum post so I can grab them out and use them to display .flv video files, the variables will always be on new lines within the $ptext variable yep Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650774 Share on other sites More sharing options...
DarkWater Posted September 25, 2008 Share Posted September 25, 2008 <?php $ptext =" lots of interesting text [store]http://www.p4tv-video.com[/store] [dir]q2 flash[/dir] [file]q2_01_aaron_town_v_ben_evans.flv[/file] [vid]video.flv[/vid] "; $regex = '`\[store\](?P<store>[^\[]+)\[/store\]\x0A\[dir\](?P<dir>[^\[]+)\[/dir\]`is'; preg_match($regex, $ptext, $matched); print_r($matched); I started the regex, but it's getting really complicated really quickly, as you can see. =P Will those parameters always be in the order 'store, dir, file, vid'? If not, this is going to be really annoying, lol. Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650784 Share on other sites More sharing options...
xxBUCKSxx Posted September 25, 2008 Author Share Posted September 25, 2008 yep always going to be in the same order just to keep things as easy as possible!! Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650797 Share on other sites More sharing options...
DarkWater Posted September 25, 2008 Share Posted September 25, 2008 <?php $ptext =" lots of interesting text [store]http://www.p4tv-video.com[/store] [dir]q2 flash[/dir] [file]q2_01_aaron_town_v_ben_evans.flv[/file] [vid]video.flv[/vid] "; $regex = ' `\[store\](?P<store>[^\[]+)\[/store\] \[dir\](?P<dir>[^\[]+)\[/dir\] \[file\](?P<file>[^\[]+)\[/file\] \[vid\](?P<vid>[^\[]+)\[/vid\]` is'; preg_match($regex, $ptext, $matched); print_r($matched); If you want to capture more than one set per $ptext, use preg_match_all() instead of preg_match(). Inside $matched, btw, there's $matched['store'], $matched['dir'], etc. for ease. Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650812 Share on other sites More sharing options...
xxBUCKSxx Posted September 25, 2008 Author Share Posted September 25, 2008 Ah! I see how it works, I have cut/pasted into my page but I'm getting and error Warning: preg_match() [function.preg-match]: Unknown modifier ' ' am i missing something really obvious?? Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650832 Share on other sites More sharing options...
redarrow Posted September 26, 2008 Share Posted September 26, 2008 works fine mate.... Talk to ur host please.................. preg_match is a built into php.ini should work report it....... <?php $ptext =" lots of interesting text [store]http://www.p4tv-video.com[/store] [dir]q2 flash[/dir] [file]q2_01_aaron_town_v_ben_evans.flv[/file] [vid]video.flv[/vid] "; $regex = ' `\[store\](?P<store>[^\[]+)\[/store\] \[dir\](?P<dir>[^\[]+)\[/dir\] \[file\](?P<file>[^\[]+)\[/file\] \[vid\](?P<vid>[^\[]+)\[/vid\]` is'; if(preg_match($regex, $ptext, $matched)){ echo"MATCHED <BR><BR>"; echo "<PRE>"; PRINT_R($matched); echo "</PRE>"; }else{ echo "NOT MATCHED"; } ?> Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650927 Share on other sites More sharing options...
discomatt Posted September 26, 2008 Share Posted September 26, 2008 This might run a little better: <pre><?php $ptext =" lots of interesting text [store]http://www.p4tv-video.com[/store] [dir]q2 flash[/dir] [file]q2_01_aaron_town_v_ben_evans.flv[/file] [vid]video.flv[/vid] "; $regex = '%\[([a-z]++)]([^[]++)\[/\1]%'; preg_match_all( $regex, $ptext, $matches, PREG_SET_ORDER ); if( empty($matches) ) die( 'Unable to find any matches?!' ); foreach( $matches as $m ) ${$m[1]} = $m[2]; var_dump( $store ); var_dump( $dir ); var_dump( $file ); var_dump( $vid ); ?></pre> Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-650938 Share on other sites More sharing options...
xxBUCKSxx Posted September 26, 2008 Author Share Posted September 26, 2008 got it!!! Many, many many many many thanks dudes!!! You've saved my life big respect you've been total superstars;) Steve 'Bucks' Link to comment https://forums.phpfreaks.com/topic/125849-solved-i-need-to-split-part-of-a-string-into-another-variable/#findComment-651100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.