Jump to content

Extract some data from a variable and put it in another


primefalcon

Recommended Posts

for example say I have a variable like so

 

<?php
$variable = "a bunch of stuff that [rofl]that isn't here[/rofl]or is it?";
?>

 

Is there anyway to extract the rofl content to a 2nd variable like

 

<?php
$variable = "a bunch of stuff that or is it?";
$variable2 = "that isn't here";
?>

 

I realize it's probably a simple case of preg matching and replacing but, that's not something I am real familiar with

ah oops, total screw up on my part, it is still too early to think :)

Try this

 

$variable = "a bunch of stuff that [rofl]that isn't here[/rofl]or is it?";
preg_match('%\[rofl\](.*?)\[/rofl\]%i', $variable, $out);
$variable = str_replace($out[0], '', $variable);
$variable2 = $out[1];

echo $variable.'<br />'.$variable2;

 

perfect thank you very much, I really need to get into preg match a little more, very powerful. I've messed with a bit of ereg and eregi but... I see what happened I do that a lot with stuff myself, forgot to escape

 

Anyhow thanks a ton that solved my problems :-)

yup and what I am looking at doing looks perfect :-)

 

 

<?php

$variable = "a bunch of stuff that [rofl]<code>that isn't here</code>[/rofl]or is it?";
preg_match('%\[rofl\](.*?)\[/rofl\]%i', $variable, $out);

$variable2 = htmlentities($out[1]);

$variable = str_replace($out[0], "$variable2", $variable);

echo "modified variable is: " . $variable;

?>

 

now I am figuring if I run that second variable through geshi before I re-insert it, it should make for a nice syntax highlight.

 

Thx again :-)

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.