ianco Posted December 31, 2011 Share Posted December 31, 2011 Hi, I want to be able to replace text, e.g. [!gallery!], that is contained in blog post and have the word 'gallery' as a variable $gallery so i can use it to fetch files/call a function. This is what I have so far: $rowblog[Content] = preg_replace("/\[\!(.*?)\!\]/","$somevariable", $rowblog[Content]); as far as I know the variable isn't being created. Any idea of a solution? Thanks Ian Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted December 31, 2011 Share Posted December 31, 2011 your pattern is fine, where is $somevariable coming from? Quote Link to comment Share on other sites More sharing options...
ianco Posted December 31, 2011 Author Share Posted December 31, 2011 $somevariable would just be what is contained between [! and !] preg_replace may not be the most appropriate function Quote Link to comment Share on other sites More sharing options...
ragax Posted December 31, 2011 Share Posted December 31, 2011 Hi ianco, From your description, I am not clear about what you want to replace with what. But let's start with the first step. It sounds to me like you want to create a variable whose name depends on the content of the [!variable!]. Here is a piece of code that does that: the variable $gallery is created on the fly, based on the match. <?php $s='text, e.g. [!gallery!], that is contained in blog post'; $p=',\[!\K[^!]+,'; $hit=preg_match($p,$s,$match); if($hit) { ${$match[0]}=$match[0]; echo $gallery; } ?> Let me know if this is a step in the right direction. It may be that this is all you need in order to build your replace? Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted December 31, 2011 Share Posted December 31, 2011 $somevariable would just be what is contained between [! and !] preg_replace may not be the most appropriate function it is the appropriate function, the second parameter of preg_replace() is for the replacement of your pattern, so $somevariable will be whatever you want to replace [!gallery!] with. Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 1, 2012 Share Posted January 1, 2012 You said you want to call a function, so maybe you want preg_replace_callback()? Quote Link to comment Share on other sites More sharing options...
ragax Posted January 1, 2012 Share Posted January 1, 2012 Yes, trying to decipher the instructions some more, it seems he may want to replace some text either with: 1. The content of a variable named by the match, e.g. $gallery, OR with 2. The result of a function named by the match, e.g. gallery() Here are solutions to both. 1. Replacing Text with Content of Variable Named by the Match <?php $s='text, e.g. [!gallery!], that is contained in blog post'; $gallery='//variable GALLERY was here//'; $p=',\[!([^!]+)!\],'; $s=preg_replace_callback($p, function ($match) {global $gallery; return ${$match[1]};}, // $gallery $s); echo $s; ?> Note that if you use this solution, you will have to declare all the other potential variables inside the function, e.g. global $otherReplacementVariable; 2. Replacing Text with Result of Function Named by the Match <?php $s='text, e.g. [!gallery!], that is contained in blog post'; $p=',\[!([^!]+)!\],'; $s=preg_replace_callback($p, function ($match) {return $match[1]();}, // calls gallery() $s); function gallery() {return '//function GALLERY was here//';} echo $s; ?> Ianco, you must have been away from your computer for the new year... Good for you! When you have a moment, can you please indicate if this works for you? Wishing you all a happy new year. Quote Link to comment Share on other sites More sharing options...
ianco Posted January 2, 2012 Author Share Posted January 2, 2012 Hey thanks for the replies. PLayful, the first example in your first reply works great and gives me the variable. I guess what I originally envisioned was using the variable in an include e.g., include($gallery.'php'), then have a function in that file. I think your second methods sound like a better idea. The second reply example 2 seems to kill the rest of my script but is a good idea and works on its own so I will have a play around and let you know Quote Link to comment Share on other sites More sharing options...
ianco Posted January 2, 2012 Author Share Posted January 2, 2012 Got it! it wasn't working because i had the function gallery() in another function. Thanks very much for the help!!!! Ian Quote Link to comment Share on other sites More sharing options...
ragax Posted January 2, 2012 Share Posted January 2, 2012 Hi Ian, Got it! Thanks very much for the help!!!! You're welcome, thrilled that it's working for you. Wishing you a fun day. Quote Link to comment Share on other sites More sharing options...
ianco Posted January 4, 2012 Author Share Posted January 4, 2012 Hi again, Although I haven't got an actual use for this yet I've been thinking. From the code that was helpfully provided, you can't put variables into the function, i.e., gallery($username). Is there a solution to this or should it actually work and I'm just being a spanner <?php $s='text, e.g. [!gallery!], that is contained in blog post'; $p=',\[!([^!]+)!\],'; $s=preg_replace_callback($p, function ($match) {return $match[1]();}, // calls gallery() $s); function gallery() {return '//function GALLERY was here//';} echo $s; ?> Quote Link to comment Share on other sites More sharing options...
ragax Posted January 5, 2012 Share Posted January 5, 2012 Hi again Ianco, I gave you a dummy function gallery() because you didn't say what you wanted it to do. You can fill it up with whatever you like. Sure, it can use a $username variable or whatever you like. Or maybe I am misunderstanding? Wishing you a fun day Quote Link to comment Share on other sites More sharing options...
ianco Posted January 6, 2012 Author Share Posted January 6, 2012 Hi, Sorry for the delay in getting back. Using the same code I just changed the function to function gallery($username){ echo "this is my ".$username; } the variable $username is available on all my pages but all I was seeing is: this is my Anyway, I'll have a play around and mark this thread as solved. Thanks Ian Quote Link to comment Share on other sites More sharing options...
ragax Posted January 6, 2012 Share Posted January 6, 2012 Yes, but watch out, in the code I gave you, gallery() has to return something, not just echo. I'm sure you're aware of that, though. Wishing you a fun weekend. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.