1

In my PHP forum i want people to insert pictures just by inserting the number/ID of the picture (which they can see in an online photoalbum)

I am looking for a function that can read a string from their posts for example

"bla bla and look at this amazing picture [IMG]234[/IMG] isn't it awesome ..."

then finds the picture in a database with the ID 234 and replaces [IMG]234[/IMG] with <img src = "path/to/image.jpg" />

preg_replace wouldn't work :( does anyone have an Idea? thanks for Your help in advance

2
  • Could you please post what you've tried so far? It feels like you're trying to get a complete solution and thats not what StackOverflow is about :) Commented Jan 26, 2012 at 11:22
  • not really a complete solution, just the key idea ;) Commented Jan 26, 2012 at 17:57

4 Answers 4

2

I would do it in this way:

$images = array();
$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) use(&$images) {
    $images[] = $matches[1];
    return '__image_' . $matches[1];
}, $post);

if (count($images)) {
    // Select images
    $imageIds = implode(',', $images);

    // DB query
    $res = mysql_query("SELECT id, path FROM post_images WHERE id IN ({$imageIds})") or die(mysql_error());

    // Replace
    while (($row = mysql_fetch_assoc($res))) {
        $post = str_replace('__image_'.$row['id'], '<img src=' . $row['path'] . ' />', $post);
    }
}

The advantage is that you make only one query to database. This is always important to minimize them to increase performance. if you don't care or you are sure that the number of images in not going to be too high, you can simple use this code:

$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) {
    $res = mysql_query("SELECT path FROM post_images WHERE id = {$matches[1]}") or die(mysql_error());
    $path = mysql_result($res, 0);
    return "<img src='$path' />";
}, $post);
Sign up to request clarification or add additional context in comments.

Comments

2

here is what i did:

$find = preg_match_all("!\[img\][0-9]+\[\/img\]!", $post, $matches);
foreach ($matches as $match) {
    foreach ($match as $ma) {
        $res = str_replace("[/img]","", str_replace("[img]", "",$ma));
        $query = "
            SELECT 
                path
            FROM
                table
            WHERE id = '".$res."'   
        ";
        $result = mysql_query($query, $conn) or die(mysql_error());
        while($line = mysql_fetch_array($result)) { 
            $path = $line["path"];  
            $path = "<img src = '".$path. "'></img>";
            $post = str_replace ("[img]" . $res . "[/img]", $path, $post);
        }
    }
}

note: i don't know why preg_match_all creates a 2 depths array

2 Comments

by the way, does anyone know if the effect, where your post fades from orange to white is a HTML5/CSS function ? and if yes, what is it called ?
It's easily implemented using jQuery animation. CSS is used to describe the transition (i.e. start orange then fade to white). jQuery handles the intermediate colours and the timing etc.
0

Your code needs to make a db call to get the path from the ID.

Use a tag parser or basic regex to match \[img][0-9]+\[/img]. (preg_match)

Then query your database for the path with the match result as the ID.

Finally, use str_replace to replace the original "[img]$match[/img]" with

Example (pseudo, look these functions up first)

$matches = preg_match_all('\[img][0-9]+\[/img]', $input);
$output = $input;
foreach ($matches as $match) {
   $cur = mysql_query("SELECT path FROM table WHERE ID = $match");
   $row = mysql_fetch_array($cur);
   $output = str_replace('[img]'.$match.'[/img]',$row['path'],$output);
}

Note: this will only work with tags in lowercasse: img and not with IMG, iMG, ImG and so on.

2 Comments

i dont understand, preg_match just returns true or false, doesn't it ?
$matches will either be 1 or 0 in your case
0

Why doesn't preg_replace work? Not knowing the the details, I would guess that you are not escaping the square brackets. What does your regular expression look like? Try something like

/\[IMG\](\d+)\[/IMG]/

The image id will then be in the group 1, i.e. $1.

Example:

preg_match_all('/\[IMG\](\d+)\[/IMG]/', $post, $matches);

foreach ($matches as $match) {
    echo "Image number: " . $match[1] . "\n";
}

2 Comments

the pattern is not the problem, i am "just" having difficulties isolating the id from the string and putting it in my query function
Added example. There are two things to be aware of here: 1. You select the number for extraction by putting that part of the regular expression in parentheses. 2. You retrieve the matches through the third parameter to preg_match[_all].

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.