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.