Jump to content

replacing text with a function or variable


ianco

Recommended Posts

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

 

 

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?

 

 

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

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.

:)

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

 

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

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

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

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.