0

I am trying scan through a string for specific tags and replace them with properly formatted html. For example I would like to replace image id with

I have this so far which scans the string and returns an array containing the id within the tags

function get_image($string, $start, $end) {
    $start = preg_quote($start, '|');
    $end = preg_quote($end, '|');
    $matches = preg_match_all('|'.$start.'([^<]*)'.$end.'|i', $string, $output);
    return $matches > 0
        ? $output[1]
        : array();
} 

$output = get_image($string,'<img>','</img>');
for($x = 0; $x < count($output); $x++){
    $id = mysqli_real_escape_string($con,$output[$x]);
    $sql = "SELECT * FROM images WHERE image_id = '$id'";
    $query = mysqli_query($con,$sql);
    $result = mysqli_fetch_assoc($query);
    $replacement = '<img src="'.$result['img_src'].'" width="'.$result['img_width'].'" height="'.$result['img_height'].'" />';
}

example of $string

Example of string would be some text like this
followed by an image
<img>1</img>
And somemore text

So I now have this array of id's which can be used to get image src width height from database. But can't work out how to replace the old tags with the new tags.

I can use a for loop to format each entry in the array but how would I replace the tags with in the new formatted text in the correct place within the string.

3
  • Try "/$start([^<]+)$end/i" as regexp. And replace </img> Through <\/img> Commented Feb 26, 2014 at 15:37
  • Give an example of the value of $string Commented Feb 26, 2014 at 15:38
  • I have added an example how the string could look, its generated using a textarea with minimal text editing options. Its just the <img> I cant seem to figure out Commented Feb 26, 2014 at 15:45

1 Answer 1

1

You can use something like this using preg_replace_callback():

// Get info of image $id
function getImageById($id){
    $sql = "SELECT * FROM images WHERE image_id = '$id'";
    return mysqli_query($con,$sql)->fetch_assoc();
}
// Proccess the info the regex gives and wants
function getImageById_regex($matches){
    // Some function to get the src by the id
    $img = getImageById( $matches[1] );
    return '<img src="'.$img['src'].'" alt="'.$img['alt'].'" />';

}
// The actual magic:
$string = preg_replace_callback("/<img>(.*?)<\/img>/", "getImageById_regex", $string);

In this version the getImageById() returns an array with info, but you can alter it and make it return the whole images html.

It can be improved:

// The actual magic, but first use a fast method to check if the slow regex is needed:
if( strpos($string, '<img>')!==false){
    $string = preg_replace_callback("/<img>(.*?)<\/img>/", "getImageById_regex", $string);
}

Tip: Look around for some BB-code scripts. They work similar

Sign up to request clarification or add additional context in comments.

2 Comments

Thats great it definitely formats the string correctly but the mysqli returns nothing
$matches[0] should be changed to $matches[1]

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.