Jump to content

[SOLVED] I need to split part of a string into another variable


xxBUCKSxx

Recommended Posts

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'

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 :)

<?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.

<?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.

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";
}
?>

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>

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.