Jump to content

Select specific snippet of text, replace with a database result


Michan

Recommended Posts

Hi,

 

I'm having some problems trying to do something; I just can't think of a way to do it. Basically, I want to search for a specific snippet of text in a body of text, then replace it with the requested database result with some thrown in HTML.

 

Users will be able to type the following (or something along these lines, depending on what works best):

[image=12345]Insert caption here[/image]

 

Then I need it to search for the requested image in the appropriate table and return the image's url (for an example, let's say the table is called "images", and the columns required are "imageid" and "imageurl".

 

And finally, it needs to be replaced in the following format:

<img src="link.to/requested/image.jpg"><BR>Insert caption here

 

I've seen it done before, so there must be a way. I just need a little help understanding that method.

 

Thanks in advance for your time.

 

- Mi

Link to comment
Share on other sites

Assuming the format: [image=12345]Insert caption here[/image]

 

Then use a preg_match combined with a database search

 

$pattern = "/\[image=([^\]]*)\]/";
preg_match($pattern,$string,$matches);
$image_id = matches[1];//this will be the id you can query in the database for the correct url

//do the query here, yadyada $row = mysql_fetch_array($query);

$pattern_find = "/\[image=([^\]]*)\]([^\[]*)\[\/image\]/";
$pattern_replace = "<img src=".$row['url']."><BR>$2";
preg_replace($pattern_find,$pattern_replace,$string);

That will do it (I may have a slight typo, but as far as my knowledge goes with this stuff, it should work).

 

$2 will be whatever is matched inside the second set of parentheses ([^\[]*), also known as the backreference (you can google this if you want more information on how it works).

Link to comment
Share on other sites

<?php
$orig = '[image=12345]Insert caption here[/image]';

$find = '~[image=(.*)](.*)[/image]~e';


if(preg_match($find,$orig,$matches)){
$num = preg_replace("~image=(.*)\](.*)~","$1",$matches[0]);
$cap = preg_replace("~image=(.*)\](.*)\[\/image~","$2",$matches[0]);
echo $num;
echo $cap;
    $sql = mysql_query("SELECT * FROM images WHERE imageid=$num");
$row = mysql_fetch_array($sql);
    echo '<img src="'.$row['imageurl'].'"><br>'.$cap;
}
?>

Link to comment
Share on other sites

<?php
$orig = '[image=12345]Insert caption here[/image]';

$find = '~[image=(.*)](.*)[/image]~e';


if(preg_match($find,$orig,$matches)){
$num = preg_replace("~image=(.*)\](.*)~","$1",$matches[0]);
$cap = preg_replace("~image=(.*)\](.*)\[\/image~","$2",$matches[0]);
echo $num;
echo $cap;
    $sql = mysql_query("SELECT * FROM images WHERE imageid=$num");
$row = mysql_fetch_array($sql);
    echo '<img src="'.$row['imageurl'].'"><br>'.$cap;
}
?>

 

Just to point out, this wouldn't work as you have to escape [ and ] (since those define range of values like [a-zA-Z0-9] if you want them to be treated literally).

Link to comment
Share on other sites

I've been spending some time implementing both of these methods, substituting some code, and even integrating them somewhat. I haven't had any success yet, though.

 

Neither of the methods seem to work for me, unfortunately. Unless I'm doing something terribly wrong.

 

Please help!

Link to comment
Share on other sites

want to post what you have?

 

I've been through many variations, but this is what I have at the moment:

 

$pattern = "~[image=(.*)]~e";
preg_match($pattern,$showarticle['artcontent'],$matches);
$image_id = $matches[1];

if($image_id)
{

$getimage = mysql_query('SELECT * FROM vg_files WHERE id ='.$image_id);

$image = mysql_fetch_array($getimage);

$pattern_find = "~[image=(.*)](.*)[/image]~e";
$pattern_replace = "<img src=".$image['file']."><BR>$2";
preg_replace($pattern_find,$pattern_replace,$showarticle['artcontent']);

}

Link to comment
Share on other sites

Okay, I have a semi-working piece of code.

 

The only problem is that it only collects the first image called. How would I get about selecting the second, third, and more?

 

$string = '[image=1234]This is a caption[/image]<BR><BR>[image=999]Caption LUL![/image]';

$pattern = "#\[image=(.*?)\]#si";
preg_match($pattern,$string,$matches);
$image_id = $matches[1];

    $sql = mysql_query("SELECT file, album FROM vg_files WHERE id=$image_id");
$row = mysql_fetch_array($sql);

$albumidstrip = explode('|', $row['album']);
$albumid = ($albumidstrip[1]);

$sql2 = mysql_query('SELECT directory FROM vg_albums WHERE id = '.$albumid);
$row2 = mysql_fetch_array($sql2);

$pattern_find = "#\[image=(.*?)\](.*?)\[\/image\]#si";
$pattern_replace = '<img src="downloads/'.$row2['directory'].'/'.$row['file'].'"><BR>$2';
$string = preg_replace($pattern_find,$pattern_replace,$string);

echo $string;

Link to comment
Share on other sites

Thank you all so much for your help! We're almost there; I can tell by changing the variable of the $matches[$i] to 1, 2, etc - as it delivers a different image for each.

 

Right now though, it's still displaying the first image above every different caption. Could somebody please tell me what little bit of code I need to add/remove/change to fix this, and get every image to show rather than just the first one?

 

$text = '[image=1234]Caption 1![/image]Blah blah[image=4321]Caption 2.[/image]';

$pattern = "#\[image=(.*?)\]#si";
preg_match_all($pattern,$text,$matches, PREG_SET_ORDER);

for ($i = 0; $i < count($matches); $i++) {

    $sql = mysql_query("SELECT file FROM vg_files WHERE id=".$matches[$i][1]);
$row = mysql_fetch_array($sql);

$pattern_find = "#\[image=(.*?)\](.*?)\[\/image\]#si";
$pattern_replace = ''.$row['file']."<BR>$2";
$text = preg_replace($pattern_find,$pattern_replace,$text);

}

 

Outputs as:

 

<img src="link.to/1234.jpg"><BR>Caption 1!Blah blah<img src="link.to/1234.jpg"><BR>Caption 2.

 

Many thanks for your help!

 

- Mi

Link to comment
Share on other sites

Okay, the problem I seem to be having is that the $i isn't being updated to 1, 2, 3 and soforth.

 

$text = '[image=1234]Caption 1![/image]Blah blah[image=4321]Caption 2.[/image]';

$pattern = "#\[image=(.*?)\]#si";
preg_match_all($pattern,$text,$matches, PREG_SET_ORDER);

for ($i = 0; $i < sizeof($matches); $i++) {

    $sql = mysql_query("SELECT file FROM vg_files WHERE id=".$matches[$i][1]);
$row = mysql_fetch_array($sql);

$pattern_find = "#\[image=(.*?)\](.*?)\[\/image\]#si";
$pattern_replace = ''.$row['file']."<BR>$2";
$text = preg_replace($pattern_find,$pattern_replace,$text);

}

 

I've been experimenting for hours now, with no success. If somebody could please guide me in the right direction by telling me what I'm doing wrong, I'd very much appreciate that.

 

Thanks in advance, once again.

 

- Mi

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.