kobanrichard Posted October 25, 2015 Share Posted October 25, 2015 I have an array element that contains a subpage's content: $page['content'] I want to do some MySQL query, if the variable's content contains this pattern: {gallery:somerandomIDnumber} The problem is, I don't want to lose the other stuff, and it's also important that the query should run where the pattern belongs. For example, this is the $page['content'] content: <h1>Title of the page</h1> {gallery:10} <p>Other informations...</p> I tried this with preg_match function, but unfortunately I can't figure it out, how I can save the other content around my {gallery:10} pattern. // Gallery include by ID preg_match('~\{gallery\:[0-9]{1,5}\}~', $page['content'], $matches); foreach($matches[0] as $value) { $int = filter_var($value, FILTER_SANITIZE_NUMBER_INT); $query = 'SELECT * '; $query .= 'FROM gallery_images '; $query .= 'WHERE gid = '.$int; $gallery = ms_query($query); //ms_query is a function I wrote myself. Unlike mysqli_query function this function doesn't require the connection parameter every single time I call it, only the query itself while($gimage = mysqli_fetch_assoc($gallery)) { echo '<img src="admin/uploaded/'.$gimage['imagepath'].'" width ="100">'; } } Summarazing, in this situation, I want to echo 1. the title of the page 2. some imagepath from my database 3. other informations Thanks in return for Your help! Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 25, 2015 Share Posted October 25, 2015 If you want the {gallery:xxxxx} string to replaced with the images in the gallery then you need to use preg_replace_callback preg_replace_callback( '~\{gallery\[0-9]{1,5})\}~' // call anonymous function for replacing {gallery:xxxxx} with gallery contents , function($matches) { // get the id from index 1 $id = intval($matches[1]); $query = 'SELECT * FROM gallery_images WHERE gid = '.$int; $gallery = ms_query($query); //ms_query is a function I wrote myself. Unlike mysqli_query function this function doesn't require the connection parameter every single time I call it, only the query itself // use concatenation for bulding the gallery contents $galleryImages = ''; while($gimage = mysqli_fetch_assoc($gallery)) { $galleryImages .= '<img src="admin/uploaded/'.$gimage['imagepath'].'" width ="100">'; } // return the gallery contents to be used as the replacement return $galleryImages; } // the subject , $page['content']); Quote Link to comment Share on other sites More sharing options...
hansford Posted October 25, 2015 Share Posted October 25, 2015 Just use a capturing group for the digits as you already know the other part. {gallery:(\d+)} Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 25, 2015 Share Posted October 25, 2015 (edited) I am not sure, but I am thinking this is a situation where the OP is asking for a fix on what he thinks is the solution to the problem/overall goal. OP, you say: I have an array element that contains a subpage's content: $page['content'] Where does this array come from or how is it created?. Please provide details and code and what exactly subpage means to you. Edited October 25, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
kobanrichard Posted October 26, 2015 Author Share Posted October 26, 2015 I am not sure, but I am thinking this is a situation where the OP is asking for a fix on what he thinks is the solution to the problem/overall goal. OP, you say: Where does this array come from or how is it created?. Please provide details and code and what exactly subpage means to you. $page['content'] is an array element which comes from the MySQL database with the mysqli_fetch_assoc() function. This array element contains this string: <h1>Title of the page</h1> {gallery:10} <p>Other informations...</p> In the end, I want to display images on a page, depending on the number between the curly brackets, like this: <h1>Title of the page</h1> <img src="admin/uploaded/someimg.jpg" /> <img src="admin/uploaded/someotherimg.jpg" /> <img src="admin/uploaded/anotherimg.jpg" /> <p>Other informations...</p> Quote Link to comment Share on other sites More sharing options...
printf Posted October 26, 2015 Share Posted October 26, 2015 (edited) There are many ways to do it, but I would just do all of it when you are building $page['content'], using some kind of join, that way you don't need to call up the database again, or use repetitive loops because your logic is BAD. But if you must do it the way you are doing it, create a function or call back function that draws out the (\d+) and then just replace {gallery:\d+} with your image paths... But use string functions, and sprintf() to do the replacement, as (40) simple string function calls uses less CPU(s) clocks than a single REGEX pattern call! example... <?php function getNumber ( $string, $p_start = '{gallery:', $p_end = '}' ) { if ( FALSE !== ( $start = strpos ( $string, $p_start ) ) && ( $end = strpos ( $string, $p_end, $start ) ) !== FALSE ) { return array ( substr ( $string, ( $start + strlen ( $p_start ) ), ( $end - ( $start + strlen ( $p_start ) ) ) ), substr ( $string, 0, $start ) . '<img src="%1$s" />' . substr ( $string, ( $end + 1 ) ) ); } return FALSE; } // string to sprintf on $string = '<h1>Title of the page</h1> {gallery:19} <p>Other informations...</p>'; // data to insert into string $from_db = 'admin/uploaded/someimg.jpg'; if ( ( $out = getNumber ( $string ) ) !== FALSE ) { echo sprintf ( $out[1], $from_db ); } ?> note... $out[0] = contains the id that you would query the database with Edited October 26, 2015 by printf Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 26, 2015 Share Posted October 26, 2015 Op, it is just as I suspected. If you provide your full and complete files along with an SQL dump of your database with some sample data we can help you get this thing done right. What you think needs to be done to accomplish your task is not it. 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.