0

I'm trying to replace a simple command from a basic CMS with a database call.

The user enters

[gallery id=x]

into the CMS, and I want this to go away, find the images included in that gallery, and display them (ready for the jQuery Cycle plugin)

I can get:

$pattern = '/\[gallery id=(\w+)\]/i';
$rpl     = 'Display Gallery ID ${1}';
$bubble  = preg_replace($pattern, $rpl, $bubble);

...which returns "Display Gallery 12" (for example). However I need it to do this:

$sql = "SELECT * FROM galleries INNER JOIN photos ON photos.PhotoGallery=galleries.GalleryID WHERE GalleryID='x'";
$set = mysql_query($sql);
echo '<div id="gallery">';
while($row = mysql_fetch_array($set))
{
 echo '<img src="'.$row['PhotoPath'].'" />';
}
echo '</div>';
7
  • 2
    The mysql_* functions will be deprecated in PHP 5.5. It is not recommended for writing new code as it will be removed in the future. Instead, either the MySQLi or PDO and be a better PHP Developer. Commented Jan 2, 2013 at 21:56
  • With that said, elaborate on something like this. Commented Jan 2, 2013 at 21:58
  • Are you just asking how to match and return the value rather than replace it? See preg_match. Commented Jan 2, 2013 at 22:02
  • I can't figure out what he's asking for. How does the query at the bottom relate to the substitution at the top? Is the question about how to get the value of x to substitute into the SQL? Commented Jan 2, 2013 at 22:03
  • @JasonMcCreary - Changed to read "need to do this". The ID number in the [gallery id=XXX] command needs to be carried through to an SQL statement (SELECT WHERE id=XXX) Commented Jan 2, 2013 at 22:04

2 Answers 2

1

You can use the preg_replace_callback function, which takes each match your regular expression finds and replaces it with the string returned from a provided callback. So you could do something like:

function generateGallery($matches) {
    // generate the string here, $matches[1] will be your gallery id
    return "Gallery Content for Gallery " . $matches[1];
}

$pattern = '/\[gallery id=(\w+)\]/i';
$bubble = preg_replace_callback($pattern, generateGallery, $bubble);
Sign up to request clarification or add additional context in comments.

4 Comments

What does this do that you can't do with an ordinary preg_replace()?
He wanted to execute SQL statements based in the id number in the matched tag and generate the replacement that way. The generateGallery callback gives him a convenient place to do that once the id is known.
But not a convenient way to echo the results of the query into the page, unless they happen to be part of the replacement.
He was using echo in his example, but what he was echoing was the replacement string.
0

Use:

preg_match($pattern, $string, $matches);

After this, the ID is in $matches[1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.