primefalcon Posted April 23, 2010 Share Posted April 23, 2010 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 https://forums.phpfreaks.com/topic/199470-extract-some-data-from-a-variable-and-put-it-in-another/ Share on other sites More sharing options...
JAY6390 Posted April 23, 2010 Share Posted April 23, 2010 <?php $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]; ?> Link to comment https://forums.phpfreaks.com/topic/199470-extract-some-data-from-a-variable-and-put-it-in-another/#findComment-1046914 Share on other sites More sharing options...
primefalcon Posted April 23, 2010 Author Share Posted April 23, 2010 Didn't quite work for me, testing that code got me a bunch stuff that [rl]that isn't here[/rl]or is it? for the first variable and the second variable was actually empty Link to comment https://forums.phpfreaks.com/topic/199470-extract-some-data-from-a-variable-and-put-it-in-another/#findComment-1046920 Share on other sites More sharing options...
JAY6390 Posted April 23, 2010 Share Posted April 23, 2010 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 https://forums.phpfreaks.com/topic/199470-extract-some-data-from-a-variable-and-put-it-in-another/#findComment-1046923 Share on other sites More sharing options...
primefalcon Posted April 23, 2010 Author Share Posted April 23, 2010 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 https://forums.phpfreaks.com/topic/199470-extract-some-data-from-a-variable-and-put-it-in-another/#findComment-1046925 Share on other sites More sharing options...
JAY6390 Posted April 23, 2010 Share Posted April 23, 2010 eek eregi! Yeah regular expressions are great Link to comment https://forums.phpfreaks.com/topic/199470-extract-some-data-from-a-variable-and-put-it-in-another/#findComment-1046928 Share on other sites More sharing options...
primefalcon Posted April 23, 2010 Author Share Posted April 23, 2010 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 https://forums.phpfreaks.com/topic/199470-extract-some-data-from-a-variable-and-put-it-in-another/#findComment-1046933 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.