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

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.