I have string with x occurrences of some substring. I need replace every occurrences of substring one by one (not all at one step) and everytime when I replace one substring, i need save this substring into variable before it will be replaced...
Exactly - I have input with text and three images in base64. I need cut base64 code of images (replace it with '') and save this img base64 code into new file and echo only input text without base64 code of imgs...
I tried for loops, while loops, functions... but everytime it replace only first occurrences of substring and not other
I think that I must save $original_string every time in loop cycle, because in my codes it always start with whole $original_string from input. But I don't know how I must do it...
$img_count = substr_count($original_string, "data:image");
for ($i = 0; $i < $img_count; $i++) {
$img_name = get_string_between($original_string, 'data-filename="', '.');
$img_base64 = get_string_between($original_string, 'src="', '"');
$original_string = str_replace($img_base64, '', $original_string);
$myfile = fopen("../img/clanky/" . $id_noveho_clanku . "/" . $img_name . ".txt", "w") or die("Unable to open file!");
$txt = $img_base64;
fwrite($myfile, $txt);
fclose($myfile);
}
while (strpos($original_string, "data:image") !== false) {
$img_name = get_string_between($original_string, 'data-filename="', '.');
$img_base64 = get_string_between($original_string, 'src="', '"');
$original_string = str_replace($img_base64, '', $original_string);
$myfile = fopen("../img/clanky/" . $id_noveho_clanku . "/" . $img_name . ".txt", "w") or die("Unable to open file!");
$txt = $img_base64;
fwrite($myfile, $txt);
fclose($myfile);
}
I want to have 3 files with base64 code of images and twxt from input without this base64. But I have only one file, and text without only first occurrence.
EDIT: so, I find out this...
I upload img "výstřižek.jpg", On my web img are render correctly, without errors. if I see on my admin site on webhosting, I see this name correctly in files, but if I connect to my host server by filezilla FTP and I open file tree in this FTP client, there I see "VýstÃÂiþek3.jpg". But why??
I cant post images here, so I upload screenshots on gdrive, please view them, for better understood of actual problem https://drive.google.com/open?id=1OQn-L1xePo6o1I6U7mmvZbxiEOBhPLvp
Yes, your code always returns the first result because it's always matching from the beginning of the string, you should remove the part you've already processed for it to work or use an offset from which to start the search in each iterarion: start with 0
and before removing the data, use its position as the next offset. You would then have to pass it to your get_string_between
function to limit the search.
But since your original string is in fact html, I'd recommend using some html parsing library to avoid headaches with attributes being on a different order, possible escaping issues, etc. This solution uses the DOM extension that should be enabled by default.
$doc = new DOMDocument();
// Treat the string as a frament and do not add a DOCTYPE or mandatory tags
$doc->loadHTML(mb_convert_encoding($original_string, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
// Get all images
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
$src = $img->getAttribute('src');
$name = $img->getAttribute('data-filename');
// Check if it contains base64 data and a filename
if (false !== mb_strpos($src, 'data:image') && "" !== $name) {
$data = mb_substr($src, strpos($src, ',') + 1); // Data start
$img->setAttribute('src', mb_str_replace($data, '', $src)); // Remove data
$name = mb_substr($name, 0, strrpos($name, '.'));
// Save the image to disk
$path = "../img/clanky/${id_noveho_clanku}/${name}.txt";
file_put_contents($path, $data);
}
}
// Get the HTML with the data removed
$parsed = $doc->saveHTML($doc);